Skip to main content

Thread: Bash and Sed with Variable


hello,

trying make bash script add "template-like" structure new xml file

code:
settings.xml    <?xml version='1.0'?><map>  </map>
i have bash script add 3 line template settings.xml

code:
newclass='''<object file="new" > \n  <string>new</string> \n  </object>'    # insert blank class  sed '1a\ $newclass' settings.xml > new_settings.xml
the problem above code outputs literal variable new_settings

code:
new_settings.xml    <?xml version='1.0'?><map>   $newclass  </map>
if try double quotes around sed argument, error
code:
sed: -e expression #1, char 29: unknown command: `<'
anyone able explain how make work?

quote posted kingbilly view post
hello,

trying make bash script add "template-like" structure new xml file

code:
settings.xml    <?xml version='1.0'?><map>  </map>
i have bash script add 3 line template settings.xml

code:
newclass='''<object file="new" > \n  <string>new</string> \n  </object>'    # insert blank class  sed '1a\ $newclass' settings.xml > new_settings.xml
the problem above code outputs literal variable new_settings

code:
new_settings.xml    <?xml version='1.0'?><map>   $newclass  </map>
if try double quotes around sed argument, error
code:
sed: -e expression #1, char 29: unknown command: `<'
anyone able explain how make work?
the quoting confusing me, seems work:

code:
newclass='''<object file="new" >\\  <string>new</string>\\  </object>'    tmpfile=/tmp/sed$$    echo "1a\\" >$tmpfile  echo "$newclass" >>$tmpfile    # insert blank class  sed -f $tmpfile settings.xml > new_settings.xml    rm -f $tmpfile
why have 3 single quotes @ beginning, way? first 2 result in empty string, can removed.


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk Bash and Sed with Variable


Ubuntu

Comments