Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 17, 2010, 05:30 PM   #1
killerham
macrumors newbie
 
Join Date: Sep 2009
Bourne Shell scripting help

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.
killerham is offline   0 Reply With Quote
Old Oct 17, 2010, 05:39 PM   #2
chown33
macrumors 603
 
Join Date: Aug 2009
Post your code.


Quote:
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.
chown33 is online now   0 Reply With Quote
Old Oct 17, 2010, 05:47 PM   #3
killerham
Thread Starter
macrumors newbie
 
Join Date: Sep 2009
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
killerham is offline   0 Reply With Quote
Old Oct 17, 2010, 06:14 PM   #4
chown33
macrumors 603
 
Join Date: Aug 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:
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".
chown33 is online now   0 Reply With Quote
Old Oct 17, 2010, 06:19 PM   #5
killerham
Thread Starter
macrumors newbie
 
Join Date: Sep 2009
I still don't get it but whatever...I'll take the hit.


thanks
killerham is offline   0 Reply With Quote
Old Oct 17, 2010, 06:33 PM   #6
killerwhack
macrumors regular
 
Join Date: Aug 2004
Location: Los Angeles, California
status code

status code is stored in "$?"

after each command is executed.
killerwhack is offline   0 Reply With Quote
Old Oct 17, 2010, 06:36 PM   #7
killerham
Thread Starter
macrumors newbie
 
Join Date: Sep 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?
killerham is offline   0 Reply With Quote
Old Oct 17, 2010, 07:40 PM   #8
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by killerham View Post
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?


Quote:
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.
chown33 is online now   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: Help needed - Shell scripting Qmaster <- Compressor 4.0 -> Mavericks BDM STUDIOS NL Mac Programming 2 Apr 8, 2014 03:57 AM
Shell Scripting or AWK? mar2194 Mac Programming 3 Dec 9, 2012 07:30 PM

Forum Jump

All times are GMT -5. The time now is 06:48 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC