Possible Sh bug?

Govind N. Kamat kamat at uceng.UC.EDU
Fri May 19 13:45:22 AEST 1989


In article <1002 at syma.sussex.ac.uk> andy at syma.sussex.ac.uk (Andy Clews) writes:

>% sh
>$ echo $?
>0
>$ if [ $? != 0 ]; then exit 1; fi
>$ echo $?
>0
>
>Why the different value of $? at the end of each example? Which system
>is at fault? Or are they both wrong :-) :-)

The Sun version is right.  The return code of an "if" is supposed to
be 0 in case none of the branches gets executed.  To avoid this
behavior of the "if", you can use

[ $? -ne 0 ] && exit 1
echo $?

which will return a 1.

>What exit status (value) should the "if" return? My understanding is
>that "exit" only works in non-interactive shells (see man sh(1)), but
>surely in the above, it shouldn't get as far as the "exit" anyway
>because $? is 0. 

Exit should work the same in both interactive and non-interactive
shells.  Perhaps your sh(1) behaves differently.
-- 
Govind N. Kamat				College of Engineering
kamat at uceng.UC.EDU			University of Cincinnati
					Cincinnati, OH 45221, USA



More information about the Comp.unix.questions mailing list