Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

killerham

macrumors newbie
Original poster
Sep 12, 2009
23
0
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.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
Post your code.


Also I tried to use $@ to get the whitespace in the arguments but that didn't work.
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.
 

killerham

macrumors newbie
Original poster
Sep 12, 2009
23
0
I tried this: by calling
Code:
#!/bin/sh

`$1`
if [ "$@" -eq 0 ];
then
        echo "Command $1 successful"
		
else
        echo "Command $1 not successful"
fi
by calling "scriptAsn echo dog"
i get the error
test: unknown operator dog

I want it to say Command echo dog successful
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
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:
Code:
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".
 

killerham

macrumors newbie
Original poster
Sep 12, 2009
23
0
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?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
I understand that part, but how would I merge each argument into one big one?

You've already seen it. Several times.

Make the following shell script. Name it 'example'.
Code:
#!/bin/sh

echo "BEFORE" "$@" "AFTER"
echo "+++" "$@" "+++"
echo 1 is "$1"
echo 2 is "$2"

Run it as follows:
Code:
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?


and how would I implement the error code into the if statements?
Study what you've already posted more carefully. Refer to a reference manual or book as needed.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.