Re: Very Simple script driving me nuts!

From: Kevin Collins (spamtotrash_at_toomuchfiction.com)
Date: 05/16/04

  • Next message: Kevin Collins: "Re: Unix: List only File names and not directory"
    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


  • Next message: Kevin Collins: "Re: Unix: List only File names and not directory"

    Relevant Pages

    • SUMMARY: Annoying Shell/Terminal Problem
      ... that a startup script was set to trapINT. ... # echo howdy^Cecho doody ... The ^C canceled the first echo, but it doesn't cause the shell ... when I run those echo commands from the shell. ...
      (SunManagers)
    • Re: take file name from command line arguement and write to file
      ... the Bourne Again Shell. ... > echo $a ... splitting, parameter substitutions etc. ... script and first sees the eval command, at first it behaves the usual ...
      (comp.unix.shell)
    • Re: help regarding shell script to find out if a directory exists at a given path
      ... echo "found directory at the given path.." ... Every line I write is potentially a line in a script. ... resulting program will be easier to maintain than a bash script. ... shell is more than powerful enough. ...
      (comp.unix.programmer)
    • Re: Mutiple fork return status
      ... from a script you can obtain the exit code of any background process you spawned with the "wait" builtin, it will return the process' exit code as its own or 127 in case of errors try this sequence of commands, they are picked from memory but should be correct: ... # echo $? ... if you issue a "wait" while the process you are inquiring about is still running, the script will pause until the process in question exits by itself or is terminated. ... you can get more info about the "wait" builtin in the docs for the shell of your choice. ...
      (comp.os.linux.misc)
    • Re: Very Simple script driving me nuts!
      ... > from shell scripting over the years, for the most part been working ... > 5 echo ... > $* so I don't have type any surrounding quotes when I use my script, ... echo -n "CMD: $*" ...
      (comp.unix.shell)