Path: utzoo!attcan!uunet!mcvax!hp4nl!philmds!leo From: leo@philmds.UUCP (Leo de Wit) Newsgroups: comp.unix.questions Subject: Re: test the exit status inside a shell Keywords: exit status Message-ID: <826@philmds.UUCP> Date: 1 Oct 88 09:18:44 GMT References: <5651@sgistl.SGI.COM> <3741@boulder.Colorado.EDU> <703@necis.UUCP> Reply-To: leo@philmds.UUCP (Leo de Wit) Organization: Philips I&E DTS Eindhoven Lines: 27 In article <5651@sgistl.SGI.COM> larry@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.