Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何使用shell脚本在文件中的特定行写入xml标记_Linux_Bash_Shell_Sed - Fatal编程技术网

Linux 如何使用shell脚本在文件中的特定行写入xml标记

Linux 如何使用shell脚本在文件中的特定行写入xml标记,linux,bash,shell,sed,Linux,Bash,Shell,Sed,我想在一个文件中的某个行号处写一个xml标记。我将要求用户填写一些信息,我将使用这些信息生成xml标记。 回音之后,我得到了想要的结果。但当我把它写入文件时,我几乎没有例外 脚本文件:test.sh cd /opt/shibboleth-ds/conf chmod 777 wayfconfig.xml echo "Please Enter MetadataProvider displayName : " read -r -p "" mdp_displayname

我想在一个文件中的某个行号处写一个xml标记。我将要求用户填写一些信息,我将使用这些信息生成xml标记。 回音之后,我得到了想要的结果。但当我把它写入文件时,我几乎没有例外

脚本文件:test.sh

cd  /opt/shibboleth-ds/conf
chmod 777 wayfconfig.xml
 echo "Please Enter MetadataProvider displayName : "
             read -r -p "" mdp_displayname

             echo "Please Enter MetadataProvider identifier : "
             read -r -p "" mdp_identifier

                 echo "Please Enter Federation Metadata URL : "
             read -r -p "" fmd_url

                 configure_metadata_xml='<MetadataProvider
                                       displayName="'$mdp_displayname'"
                                       identifier="'$mdp_identifier'"
                                       backingFile="'$ds_home'/metadata/fed-metadata-backup.xml"
                                       url="'$fmd_url'"/>'


            echo  $configure_metadata_xml
             #sed -i '70i\test string' wayfconfig.xml
            sed -i '70i\'$configure_metadata_xml'' wayfconfig.xml
cd/opt/shibboleth ds/conf
chmod 777 wayfconfig.xml
echo“请输入MetadataProvider显示名称:”
读取-r-p“mdp\U显示名称
echo“请输入元数据提供程序标识符:”
读取-r-p“mdp_标识符
echo“请输入联盟元数据URL:”
读取-r-p“fmd_url
配置\u元数据\u xml=“”
echo$configure\u metadata\u xml
#sed-i'70i\test string'wayfconfig.xml
sed-i'70i\'$configure\u metadata\u xml''wayfconfig.xml
我试图在第70行的wayfconfig.xml中写入xml标记

输出:

./test.sh
    Please Enter MetadataProvider displayName :
    MyDisplayName
    Please Enter MetadataProvider identifier :
    MyIdentifier
    Please Enter Federation Metadata URL :
    http://plamakerandroid.com/file.xml
    <MetadataProvider displayName="MyDisplayName" identifier="MyIdentifier" backingFile="/metadata/fed-metadata-backup.xml" url="http://plamakerandroid.com/file.xml"/>
    sed: can't read displayName="MyDisplayName": No such file or directory
    sed: can't read identifier="MyIdentifier": No such file or directory
    sed: can't read backingFile="/metadata/fed-metadata-backup.xml": No such file or directory
    sed: can't read url="http://plamakerandroid.com/file.xml"/>: No such file or directory
/test.sh
请输入MetadataProvider displayName:
MyDisplayName
请输入元数据提供程序标识符:
我的标识符
请输入联合元数据URL:
http://plamakerandroid.com/file.xml
sed:无法读取displayName=“MyDisplayName”:没有这样的文件或目录
sed:cannotreadidentifier=“MyIdentifier”:没有这样的文件或目录
sed:can't read backingFile=“/metadata/fed metadata backup.xml”:没有这样的文件或目录
sed:无法读取url=”http://plamakerandroid.com/file.xml“/>:没有这样的文件或目录

这里我遗漏了什么

您的变量将包含空格,因此您需要引用它。试试这个:

sed -i '70i'"$configure_metadata_xml" wayfconfig.xml
让我向您展示它是如何工作的:

测试变量中没有空格不需要引号:

test="abc"; sed -re '1i'$test <<< $'a\nb\nc'
abc
a
b
c

test=“abc”;sed-re'1i'$test您的变量将包含空格,因此需要引用它。试试这个:

sed -i '70i'"$configure_metadata_xml" wayfconfig.xml
让我向您展示它是如何工作的:

测试变量中没有空格不需要引号:

test="abc"; sed -re '1i'$test <<< $'a\nb\nc'
abc
a
b
c

test=“abc”;sed-re'1i'$test使用
awk
命令

awk 'NR==7{print "This is the new line"}7' filename
sed -i .bak '7i\
This is the new line\
' filename
使用
sed
命令

awk 'NR==7{print "This is the new line"}7' filename
sed -i .bak '7i\
This is the new line\
' filename

这些命令带有insert这是行号7处的新行带有
awk
命令

awk 'NR==7{print "This is the new line"}7' filename
sed -i .bak '7i\
This is the new line\
' filename
使用
sed
命令

awk 'NR==7{print "This is the new line"}7' filename
sed -i .bak '7i\
This is the new line\
' filename

这些命令带有insert这是行号为7的新行

OP想要添加一行,而不是替换它。OP想要添加一行,而不是替换它。configure_metadata_xml=''sed-i'70i'''$configure_metadata_xml“wayfconfig.xmlcigure_metadata_xml='sed-i'70i'$configure_metadata_xml”wayfconfig.xml