larry@sgistl.SGI.COM (Larry Autry) (09/28/88)
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. -- Larry Autry larry@sgistl.sgi.com or {ucbvax,sun,ames,pyramid,decwrl}!sgi!sgistl!larry
wu@spot.Colorado.EDU (WU SHI-KUEI) (09/29/88)
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. The shell variable $? is set to the exit status of the last executed command. Just a guest here - In real life Carl Brandauer {ncar|stcvax}!nbires!bdaemon!carl
pbush@necis.UUCP (Paul Bush) (09/29/88)
In article <3741@boulder.Colorado.EDU> wu@spot.Colorado.EDU (WU SHI-KUEI) writes: >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. > >The shell variable $? is set to the exit status of the last executed command. > The above variable '$?' works fine for the Bourne shell (sh). The equivilant C-shell variable is '$status'. Good Luck Paul Bush (necis!pbush)
nate@altos86.UUCP (Nathaniel Ingersoll) (09/30/88)
In article <3741@boulder.Colorado.EDU> wu@spot.Colorado.EDU (WU SHI-KUEI) writes: >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. > >The shell variable $? is set to the exit status of the last executed command. > >Carl Brandauer In csh, the shell variable $status is set to the exit status of the last command. -- Nathaniel Ingersoll Altos Computer Systems, SJ CA ...!ucbvax!sun!altos86!nate altos86!nate@sun.com
leo@philmds.UUCP (Leo de Wit) (10/01/88)
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.
dce@mips.COM (David Elliott) (10/02/88)
In article <826@philmds.UUCP> leo@philmds.UUCP (Leo de Wit) writes: >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 ^^ ^^^^ "if" in sh uses "then" and "fi", not "do" and "done", but that's not what I followed up for. An interesting note about while and until is that the actual syntax is while list; do list; done where a "list" is a list of commands (the semicolons can be newlines). This means that the loop a=no while echo "Enter a new value for a: \c" [ "$a" != "no" ] do read a echo "Entered $a" done is a legal loop. I've only seen this used once, but it could be useful as fast machines make it more reasonable to use sh to write interactive commands. -- David Elliott dce@mips.com or {ames,prls,pyramid,decwrl}!mips!dce