MACSCRPT Archives

June 2009

MACSCRPT@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
"Mark J. Reed" <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Mon, 1 Jun 2009 17:20:36 -0400
Content-Type:
text/plain
Parts/Attachments:
text/plain (140 lines)
No. S=`myscript.sh` assigns the *output* of myscript.sh to S.  To get
the exit code you have to use the special variable $?.

myscript.sh
case $? in
somevalue) someaction;;
othervalue) otheraction;;
*) defaultaction;;
esac

Of course if you only care about success vs failure, tests for that
are built in:

if myscript.sh; then
    It succeeded
else
   It failed
fi

On 6/1/09, David Livesay <[log in to unmask]> wrote:
> Those aren't error codes; they're known as "exit status" or "exit condition"
> codes. The convention is to use 0 to indicate an uneventful exit, usually by
> ending the script with "exit 0", and higher numbers if the script needs to
> exit without completing its intended purpose, for example:
>
> if [ ! -d $SRC_PATH ]; then
> 	echo "The source directory was not found at $PWD/$SRC_PATH."
> 	exit 1
> fi
>
> Here I'm returning a status 1, which the calling process can potentially
> respond to. If the calling process is you, sitting at your terminal, you can
> respond by remembering that you need to run that other shell script that
> downloads the source files and builds a directory. If it's another script,
> it can do the same thing by checking the value of the status code. For
> example, "if [ $? -gt 0 ]; then" would allow you to branch to an
> error-recovery routine. $? is the default variable used for exit status, but
> you can also assign one explicitly when you call a script, e.g.
> "S=`myscript.sh`". You can also get more elaborate with your status checking
> by using a case statement:
>
> case $S in
> 0)
> 	echo "Hey, it worked!"
> ;;
> 1)
> 	echo "Oh darn. The server was down."
> ;;
> 2)
> 	echo "You get the picture..."
> ;;
> esac
>
>
> On Monday, June 01, 2009, at 02:57PM, "Anthony Adachi"
> <[log in to unmask]> wrote:
>>Hello,
>>
>>In AppleScript and many other programming languages one can throw
>>deliberate errors of a specific kind as well as catch-trap for specific
>>errors. I'm having a hard time determining how to do this in bash 3.2
>>with it's 'trap' command.
>>
>>For instance, a very simple example in AppleScript...
>>
>>try
>>	error 99
>>on error 99
>>	say "Caught expected error."
>>end try
>>
>>say "doing more tests..."
>>
>>
>>Moreover, I can't figure out whether or not if it's even possible to
>>catch for specific thrown errors defined by a scripter in bash. My own
>>tests have led to a dead end and I haven't come any better understanding
>>from reading the bash info docs, and numerous online and in print
>>examples of the 'trap' command.
>>
>>
>>Basically, I wish to...
>>
>>1. First of all, have scripts throw a specifically, identifiable, unique
>>error when a certain known bad state has been encountered that they
>>can't handle.
>>
>>2. Secondly, to able to test for whether scripts are throwing an
>>specific error under a certain condition.
>>
>>
>>3. Trap for and catch only that specific error and then keep on going,
>>performing further automated tests.
>>
>>Is this possible in bash?
>>
>>
>>Below is an example which might better illustrate what I've attempted to
>>do. It unfortunately, doesn't do what's needed at the moment. i.e.- It
>>will catch all errors and exits but not a specific one.
>>
>>#--- The example below traps all errors but not the specific one.
>>
>>particular_function()
>>{
>>  current_status="some known bad condition"
>>  if [ "$current_status" = "some known bad condition" ]; then
>>    # Encountered some known condition we can't handle so throw specific
>>error.
>>    echo "Encountered a, we knew this would happen, type error."
>>    exit -99
>>  fi
>>}
>>
>>test_method_for_expected_error()
>>{
>>  trap 'echo "function threw expected error code as it should."' EXIT
>>  particular_function
>>}
>>
>>test_method_for_expected_error
>>
>>echo "Continued passed caught error as desired and continuing further
>>tests..."
>>
>>#--- end of example
>>
>>
>>Thanks,
>>
>>-Anthony
>>
>>
>

-- 
Sent from my mobile device

Mark J. Reed <[log in to unmask]>

ATOM RSS1 RSS2