[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bash script - pass command line arg to embedded sed script with multiple args



On Sat, Apr 17, 2010 at 12:02:40PM -0400, Daniel D Jones wrote:
> On Saturday 17 April 2010 00:09:28 Michael Elkins wrote:
> > On Fri, Apr 16, 2010 at 08:15:38PM -0400, Daniel D Jones wrote:
> > >What I'm trying to do is pretty simple.  Getting it to work is turning out
> > > not to be.  What I want to do is call a bash script with a couple of
> > > arguments, and, within the script, call sed to use those args to replace
> > > two placeholders in a file:
> > >
> > >bashscript SUB1 SUB2
> > >
> > >This line inside bashscript doesn't work:
> > >
> > >sed -e 's/PLACEHOLDER1/$1/' -e 's/PLACEHOLDER2/$2/' < input > output
> > 
> > If you switch the single quotes to double quotes it will work as you
> >  expect. Variables inside of double quotes are expanded.  Single quotes are
> >  for literal strings, as you've discovered.
> 
> That was the first thing I tried and sed gave me an error:
> 
> sed: -e expression #1, char 18: unknown option to `s'
> 
> I just went back and tried it again and it worked, so I have no idea what I 
> did the first time that made it not work.

As others have mentioned, rgw command-line parameter $1 probably has a
'/' in it.

A simple workaround is to use a different character as the separator.
That is: *if* you can assume that variable will not contain the
character '|', you can use:

  sed -e "s|PLACEHOLDER1|$1|' -e "s|PLACEHOLDER2|$2|" < input > output

You can use some other characters there as well. See sed(1).

-- 
Tzafrir Cohen         | tzafrir@jabber.org | VIM is
http://tzafrir.org.il |                    | a Mutt's
tzafrir@cohens.org.il |                    |  best
tzafrir@debian.org    |                    | friend


Reply to: