Bourne Shell scripting help

Discussion in 'Mac Programming' started by killerham, Oct 17, 2010.

  1. macrumors newbie

    Sep 12, 2009
    I have Write a Bourne shell script that accepts a command as an argument, runs the command and outputs a message which tells the user whether the command was successful or not. For example :
    >program cd
    command cd successful

    >program ls gfdgsfgds
    command ls gfdgsfgds not successful

    My problem is that I don't know how to get the error codes after the argument has run. Also I tried to use $@ to get the whitespace in the arguments but that didn't work. Any help would be appreciated because I have to do this for an assignment that I've been working on for a couple of days.
  2. macrumors 604

    Aug 9, 2009
    Post your code.

    I have no idea what this means.

    1. Explain what you want to happen.
    2. Show the code that tries to make it happen.
    3. Explain what actually happened.

    You should also know that $@ differs from "$@", as explained in the bash man page.
  3. thread starter macrumors newbie

    Sep 12, 2009
    I tried this: by calling
    if [ "$@" -eq 0 ];
            echo "Command $1 successful"
            echo "Command $1 not successful"
    by calling "scriptAsn echo dog"
    i get the error
    test: unknown operator dog

    I want it to say Command echo dog successful
  4. macrumors 604

    Aug 9, 2009
    The "$@" in that position makes no sense at all.

    Do you understand what $@ is? It's the list of input arguments. So when you run your 'program' script as:
    program ls -l flummox
    then "$@" is the list of arguments "ls" "-l" "flummox". So given this example, write down what "$@" expands to in the case of 'program echo dog'.

    You should make a shell script that simply echo's its input args, one per line. This is a useful exercise in learning shell scripting, and is also a useful diagnostic tool.

    I think the shell variable to use instead of "$@" is $?. Again, you would find this in the bash man page, or in any decent bash reference book.

    And your `$1` is also wrong, as it fails to pass any arguments to the $1 command. You should notice this in the output, as 'program echo dog' would not actually echo the word "dog".
  5. thread starter macrumors newbie

    Sep 12, 2009
    I still don't get it but whatever...I'll take the hit.

  6. macrumors regular


    Aug 5, 2004
    Los Angeles, California
    status code

    status code is stored in "$?"

    after each command is executed.
  7. thread starter macrumors newbie

    Sep 12, 2009
    I understand that part, but how would I merge each argument into one big one? and how would I implement the error code into the if statements?
  8. macrumors 604

    Aug 9, 2009
    You've already seen it. Several times.

    Make the following shell script. Name it 'example'.
    echo "BEFORE" "$@" "AFTER"
    echo "+++" "$@" "+++"
    echo 1 is "$1"
    echo 2 is "$2"
    Run it as follows:
    example test
    example echo dog
    example echo whiskey tango foxtrot
    example ls -l ~/Library
    Observe each run's output carefully. What does it suggest to you?

    Study what you've already posted more carefully. Refer to a reference manual or book as needed.

Share This Page