PDA

View Full Version : Bourne Shell scripting help




killerham
Oct 17, 2010, 05:30 PM
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
Oct 17, 2010, 05:39 PM
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
Oct 17, 2010, 05:47 PM
I tried this: by calling

#!/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
Oct 17, 2010, 06:14 PM
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".

killerham
Oct 17, 2010, 06:19 PM
I still don't get it but whatever...I'll take the hit.


thanks

killerwhack
Oct 17, 2010, 06:33 PM
status code is stored in "$?"

after each command is executed.

killerham
Oct 17, 2010, 06:36 PM
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
Oct 17, 2010, 07:40 PM
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'.
#!/bin/sh

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?


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.