Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > quoting in perl command in shell script

Reply
Thread Tools

quoting in perl command in shell script

 
 
rduke15
Guest
Posts: n/a
 
      05-21-2004
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...

 
Reply With Quote
 
 
 
 
Barry Margolin
Guest
Posts: n/a
 
      05-21-2004
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 ***
 
Reply With Quote
 
 
 
 
rduke15
Guest
Posts: n/a
 
      05-21-2004
>>$ 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.

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      05-24-2004
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
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      05-24-2004

Quoth (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]
 
Reply With Quote
 
Stephane CHAZELAS
Guest
Posts: n/a
 
      05-24-2004
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
 
Reply With Quote
 
Stephane CHAZELAS
Guest
Posts: n/a
 
      05-24-2004
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
 
Reply With Quote
 
Stephane CHAZELAS
Guest
Posts: n/a
 
      05-24-2004
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help integrating a perl command into a shell script. Daryl Rose Perl Misc 5 12-14-2007 12:17 AM
execute a shell script in a shell script moongeegee Perl Misc 2 12-04-2007 12:18 AM
Perl CGI script to emulate a shell command line window Alain Star Perl Misc 7 01-18-2005 03:20 PM
Shell quoting as part of the standard library? David M. Wilson Python 6 12-14-2003 07:17 PM
Using the shell's "fmt" command in a perl script Ranyart Perl Misc 5 08-30-2003 10:38 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57