[sclug] Escaping slashes in bash

Tim Sutton tim at linfiniti.com
Tue Sep 27 22:22:39 UTC 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi

Matt wrote:
> * David Kerrawn (sclug) <sclug at kerrawn.co.uk> [2005-09-27 18:34:57]:
> 
>>>-----Original Message-----
>>>From: sclug-bounces at sclug.org.uk [mailto:sclug-bounces at sclug.org.uk]On
>>>Behalf Of Peter Brewer
>>>Sent: 27 September 2005 17:29
>>>To: sclug at sclug.org.uk
>>>Subject: [sclug] Escaping slashes in bash
>>>
>>>
>>>Dear SCLUG
>>>
>>>Having some problems with escaping "/" in Bash.  We want to run perl 
>>>-pie in a bash script but have had to first put the substitution string 
>>>into a variable otherwise it doesn't get created properly.  This works 
>>>fine when there are no "/" in the substitution string but how do we get 
>>>it to work if there IS a "/"????
>>>
>>>e.g:
>>>WORKDIR="/tmp"
>>>LOCALITIESFILE="foobar.txt"
>>>CMD="s/\[LOCALITIESFILE]/${WORKDIR}\/${LOCALITIESFILE}/g"
>>>`perl -p -i -e ${CMD} ${FILENAME1}`
>>>
>>>Many thanks once again for your combined help!
>>>
>>>Cheers
>>>
>>>Pete
>>>
>>
>>I think WORKDIR="\\/tmp" should do what you want.
>>
>>$ WORKDIR="\\/tmp"
>>$ echo $WORKDIR
>>\/tmp
>>$ CMD="s/\[LOCALITIESFILE]/${WORKDIR}\/${LOCALITIESFILE}/g"
>>$ echo $CMD
>>s/\[LOCALITIESFILE]/\/tmp\/foobar.txt/g
> 
> 
> Alternatively, to avoid any backslash-escaping mess, why not simply use
> a different delimiter for the s/// operator? For example:
> 
> s|foo|bar|g
> 
> Although humour me, what is the original example supposed to be doing
> anyway?
> 
> Matt



I work with Pete so Ill give you some background as he is off napping in
his Lazy Boy sofa no doubt...

The script is a bash script

a) connects to a datbase
b) dumps some data in a text file which is named after the value of one
of the fields in the databas
c) copies a template control file for a modelling application
d) replaces placeholder tokens in the control file , including some
tokes which need to be replaced with full path and file names
e) lauches the modelling application using the generated control file
and the dumped database file


Using a different delimiter might do the job thanks, we'll give it a try!


Regards

Tim Sutton

> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> sclug mailing list
> sclug at sclug.org.uk
> http://www.sclug.org.uk/mailman/listinfo/sclug

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDOdQrWvXTJUo0BDoRAj1IAJ4lwQrFeRGhTgJRpeRJT9zEphZNBACfW4rU
nRwyd2pZoo/SoSzzN4b2f2I=
=j7wV
-----END PGP SIGNATURE-----


More information about the Sclug mailing list