test the exit status inside a shell

Leo de Wit leo at philmds.UUCP
Sat Oct 1 19:18:44 AEST 1988


In article <5651 at sgistl.SGI.COM> larry at sgistl.SGI.COM (Larry Autry) writes:
>Is there a way to test the exit status of the previously executed process
>while in a shell?  In other words, if the previous program returned an exit
>status of (2) (or even (0)), then perform something based upon that return 
>value.

As some people already mentioned, you can use $? in the sh and $status
in the csh which gives you the exit status of the previous executed
process.

If you're merely interested whether it returned a success status (0) or
failure (!0), there are some other useful constructs. In the sh
(command being now a commandlist, then a pipeline etc. depending on the
case; check your manual for the constructs possible):

command1 || command2               # do command2 only if command1 failed
command1 && command2               # do command2 only if command1 succeeded
while command1; do commands; done  # while command1 succeeds do the commands
until command1; do commands; done  # until command1 succeeds do the commands
if command1; do commands; done     # if command1 succeeds do the commands

And now we're at it:

set -e    # exit - if the shell's not interactive - as soon as a command fails

Hope this helps -
                   Leo.



More information about the Comp.unix.questions mailing list