Register
It is currently Fri Dec 19, 2014 5:14 am

escaping variable data


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Mon Jun 25, 2007 8:44 am   

Joined: Mon Jun 25, 2007 8:33 am
Posts: 4
I have written a bash script that configures INI settings for me.
The work horse behind it is sed.

Here is what I am doing:
Code:
sed -i 's/.*'$iniKey'=.*/'$iniKey'='$iniValue'/' $iniFile


This works except when my variables contain characters that need to be escaped.
For example: $iniValue=/usr/local/bin

Is there anyway to get this working... maybe by automaticaly escaping somehow?


Top
 Profile  
 PostPosted: Mon Jun 25, 2007 10:52 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
hey bpetty

i try to stay away from things that have to be escaped... but thankfully there is an easy fix for what you are trying to accomplish
sed will allow you to use any delimiter in it's call...
so
sed -i 's/.*'$iniKey'=.*/'$iniKey'='$iniValue'/' $iniFile
is the same as
sed -i 's#.*'$iniKey'=.*#'$iniKey'='$iniValue'#' $iniFile
is the same as
sed -i 's|.*'$iniKey'=.*|'$iniKey'='$iniValue'|' $iniFile

see if that bit of info can help you on your way to a solution :)


Top
 Profile  
 PostPosted: Sun Oct 21, 2007 4:23 am   

Joined: Fri Oct 19, 2007 3:09 pm
Posts: 3
I hope this helps ..

Code:
appro@stovokor ~ $ var='a/b/c/d'
appro@stovokor ~ $ echo 'a/b/c/d' | sed "s/${var}/foo/g"
sed: -e expression #1, char 7: unknown option to `s'
bash: echo: write error: Broken pipe
appro@stovokor ~ $ echo 'a/b/c/d' | sed "s/${var//\//\\/}/foo/g"
foo


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP