0

I want to write an xml tag in a file at some line number.I will be requesting the user to fill out few information which i will use to generate the xml tag. After echoing it , i got the desired out. But when i write it to file im getting few exceptions.

Script file : 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

Im trying to write the xml tag in wayfconfig.xml at line number 70.

Output:

./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

What am i missing here

2 Answers 2

3

Your variable will contain spaces, so you need to quote it. try this:

sed -i '70i'"$configure_metadata_xml" wayfconfig.xml

Let me show you how it works:

No space in the test variable no quote needed:

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

With space in the test variable but without quoting:

test="a bc"; sed -re '1i'$test <<< $'a\nb\nc'
sed: can't read bc: No such file or directory

With space in the test variable and quoting:

test="a bc"; sed -re '1i'"$test" <<< $'a\nb\nc'
a bc
a
b
c
Sign up to request clarification or add additional context in comments.

1 Comment

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

With awk command,

awk 'NR==7{print "This is the new line"}7' filename

With sed command,

sed -i .bak '7i\
This is the new line\
' filename

these commands with insert This is the new line at line number 7.

1 Comment

OP wants to add a line, not replace it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.