quoting in perl command in shell script

Discussion in 'Perl Misc' started by rduke15, May 21, 2004.

  1. rduke15

    rduke15 Guest

    Hi,

    I have a weird problem with this simple bash script calling a Perl
    command in a variable. I don't understand what's wrong.

    $ cat test.sh

    #!/bin/sh
    set -x
    cmd="perl -e 'print 1+2'"
    $cmd

    $ ./test.sh

    + cmd=perl -e 'print 1+2'
    + perl -e 'print 1+2'
    Can't find string terminator "'" anywhere before EOF at -e line 1.


    I also tried all sorts of variations with the quotes, but nothing seems
    to work.

    Thanks for any help...
    rduke15, May 21, 2004
    #1
    1. Advertising

  2. In article <c8lj0q$dn9$>,
    rduke15 <rduke15@hotmail__.__com> wrote:

    > Hi,
    >
    > I have a weird problem with this simple bash script calling a Perl
    > command in a variable. I don't understand what's wrong.
    >
    > $ cat test.sh
    >
    > #!/bin/sh
    > set -x
    > cmd="perl -e 'print 1+2'"
    > $cmd
    >
    > $ ./test.sh
    >
    > + cmd=perl -e 'print 1+2'
    > + perl -e 'print 1+2'
    > Can't find string terminator "'" anywhere before EOF at -e line 1.
    >
    >
    > I also tried all sorts of variations with the quotes, but nothing seems
    > to work.


    Try: eval $cmd

    --
    Barry Margolin,
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    Barry Margolin, May 21, 2004
    #2
    1. Advertising

  3. rduke15

    rduke15 Guest

    >>$ cat test.sh
    >>
    >> #!/bin/sh
    >> set -x
    >> cmd="perl -e 'print 1+2'"
    >> $cmd
    >>
    >>$ ./test.sh
    >>
    >> + cmd=perl -e 'print 1+2'
    >> + perl -e 'print 1+2'
    >> Can't find string terminator "'" anywhere before EOF at -e line 1.
    >>
    >>
    >>I also tried all sorts of variations with the quotes, but nothing seems
    >>to work.

    >
    >
    > Try: eval $cmd


    Yes, that works!

    Thanks a lot.
    rduke15, May 21, 2004
    #3
  4. rduke15

    Anno Siegel Guest

    Stephane CHAZELAS <> wrote in comp.lang.perl.misc:
    > 2004-05-21, 15:26(-04), Barry Margolin:
    > [...]
    > >> cmd="perl -e 'print 1+2'"
    > >> $cmd

    > [...]
    > > Try: eval $cmd

    >
    > Please get used to:
    >
    > eval "$cmd"


    Why?

    Anno
    Anno Siegel, May 24, 2004
    #4
  5. rduke15

    Ben Morrow Guest

    Quoth -berlin.de (Anno Siegel):
    > Stephane CHAZELAS <> wrote in comp.lang.perl.misc:
    > > 2004-05-21, 15:26(-04), Barry Margolin:
    > > [...]
    > > >> cmd="perl -e 'print 1+2'"
    > > >> $cmd

    > > [...]
    > > > Try: eval $cmd

    > >
    > > Please get used to:
    > >
    > > eval "$cmd"

    >
    > Why?


    Shell, not Perl. Presumably its more portable.

    Ben

    --
    Every twenty-four hours about 34k children die from the effects of poverty.
    Meanwhile, the latest estimate is that 2800 people died on 9/11, so it's like
    that image, that ghastly, grey-billowing, double-barrelled fall, repeated
    twelve times every day. Full of children. [Iain Banks]
    Ben Morrow, May 24, 2004
    #5
  6. 2004-05-21, 15:26(-04), Barry Margolin:
    [...]
    >> cmd="perl -e 'print 1+2'"
    >> $cmd

    [...]
    > Try: eval $cmd


    Please get used to:

    eval "$cmd"

    --
    Stephane
    Stephane CHAZELAS, May 24, 2004
    #6
  7. 2004-05-24, 09:19(+00), Anno Siegel:
    > Stephane CHAZELAS <> wrote in comp.lang.perl.misc:
    >> 2004-05-21, 15:26(-04), Barry Margolin:
    >> [...]
    >> >> cmd="perl -e 'print 1+2'"
    >> >> $cmd

    >> [...]
    >> > Try: eval $cmd

    >>
    >> Please get used to:
    >>
    >> eval "$cmd"

    >
    > Why?


    Because, in Bourne like shells, an unquoted variable is a list
    of file patterns not a string (it's fixed in zsh).

    If a variable is not quoted, upon expansion, it is splitted
    according to the value of the IFS special parameter, and any
    wildcards are expanded.

    For instance,

    cmd="echo 'a b'"
    eval $cmd

    outputs "a b", because $cmd is splitted into "echo", "'a" and
    "b'". eval concatenates those with spaces, so runs:

    echo 'a b'

    You would think:

    cmd='echo "*"'
    eval $cmd

    harmless?

    Try it after:

    touch './""; rm -rf "$HOME"'

    (DON'T!)

    --
    Stephane
    Stephane CHAZELAS, May 24, 2004
    #7
  8. 2004-05-24, 09:31(+00), Ben Morrow:
    [...]
    >> > > Try: eval $cmd
    >> >
    >> > Please get used to:
    >> >
    >> > eval "$cmd"

    >>
    >> Why?

    >
    > Shell, not Perl. Presumably its more portable.


    No, it's because

    eval $cmd
    in shell, would be the equivalent of perl's:

    eval(join(" ", map(glob, split($IFS_regexp, $cmd))))

    IT'S ALMOST ALWAYS WRONG TO LEAVE A VARIABLE UNQUOTED IN SHELL
    SCRIPTS.

    --
    Stephane
    Stephane CHAZELAS, May 24, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    5,696
    Jonathan Bromley
    Feb 18, 2005
  2. David M. Wilson
    Replies:
    6
    Views:
    336
    David M. Wilson
    Dec 14, 2003
  3. Ranyart
    Replies:
    5
    Views:
    163
    James Willmore
    Aug 30, 2003
  4. Alain Star
    Replies:
    7
    Views:
    167
    Tad McClellan
    Jan 18, 2005
  5. moongeegee

    execute a shell script in a shell script

    moongeegee, Dec 3, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    248
    Ben Morrow
    Dec 4, 2007
Loading...

Share This Page