Re: Very Simple script driving me nuts!
From: Kevin Collins (spamtotrash_at_toomuchfiction.com)
Date: 05/16/04
- Previous message: Kevin Collins: "Re: Very Simple script driving me nuts!"
- In reply to: Ed Morton: "Re: Very Simple script driving me nuts!"
- Next in thread: Ed Morton: "Re: Very Simple script driving me nuts!"
- Reply: Ed Morton: "Re: Very Simple script driving me nuts!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 15 May 2004 22:54:47 GMT
In article <8bSdncQ5mL9l6TvdRVn-jw@comcast.com>, Ed Morton wrote:
>
>
> Trent Curry wrote:
>> I know I am missing something really trivial here. I've bene away from
>> shell scripting over the years, for the most part been working with the
>> likes of Perl, C++, and Java, and I seem to have forgotten some aspect
>> of shell programming it seems thats biting me in the arse, when I wanted
>> to write an utterly simple script to save me some time.
>>
>> sendcmd:
>> 1 #!/bin/sh
>> 2
>> 3 CMD="./sendcommand blah 127.0.0.1 2701 '$*'"
>> 4 echo -n "CMD: $*"
>> 5 echo
>> 6 $CMD
>> 7 echo
>>
>> Say I run it like this, the echo looks fine
>
> No it doesn't. It'd look fine if you saw "do this" appear twice wsince
> you're trying to include $* twice, once at line 3 and again at line 4.
Nope - line 4 is missing the '$' from the variable named CMD! It says 'CMD' and
it should say '$CMD'. You are right about not needing the '$*' on line 4,
though.
>> $ ./sendcmd do this
>> CMD: do this
>
> In the above, you're seeing "do this" becau8se of the $* on line 4 of
> your script. The '$*' at line 3 is evaluating to '' since $* isn't
> expanded within single quotes. That same effect is causing your other
> problems.
That is just wrong. Any variable inside double-quotes is ALWAYS interpolated,
regardless of single-quotes because the whole string is double-quoted. Did you
test this? I just did with bash and sh - works as expected: all arguments are
in a single quoted string
>> but sendcommand ask as if no params were passed. If I remove the ''
>> around $* on line 3, then it works, but I need literal single quotes
>> aroudn the params being pased, so to allow spaces. I wanted to use $* so
>> I don't have type any surrounding quotes when I use my script, and have
>> the script put singles around that. (Notice I didnt put any '' around
>> "do this" on the command line.
>
> Try this:
>
> CMD="./sendcommand blah 127.0.0.1 2701 \"$*\""
> eval $CMD
As I said above, the single-quote syntax is fine...
I copied the original script and changed "sendcommand" to "echo". I then ran
the script. It works as expected except for the missing '$' on line 4 I already
mentioned:
#!/bin/sh
CMD="echo blah 127.0.0.1 2701 '$*'"
echo -n "CMD: $*"
echo
$CMD
echo
When I run it, I see:
$ ./sendcmd a b c
CMD: a b c
blah 127.0.0.1 2701 'a b c'
I can run it with shell debugging and I see:
$ sh -vx ./sendcmd a b c
#!/bin/sh
CMD="echo blah 127.0.0.1 2701 '$*'"
+ CMD=echo blah 127.0.0.1 2701 'a b c'
echo -n "CMD: $*"
+ echo -n 'CMD: a b c'
CMD: a b cecho
+ echo
$CMD
+ echo blah 127.0.0.1 2701 ''\''a' b 'c'\'''
blah 127.0.0.1 2701 'a b c'
echo
+ echo
Sooo.. Not sure why it isn't behaving as expected. Give it a try yourself with
'sh -vx' and see what you find out. Better yet, post it back.
Kevin
- Previous message: Kevin Collins: "Re: Very Simple script driving me nuts!"
- In reply to: Ed Morton: "Re: Very Simple script driving me nuts!"
- Next in thread: Ed Morton: "Re: Very Simple script driving me nuts!"
- Reply: Ed Morton: "Re: Very Simple script driving me nuts!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|