[comp.unix.questions] Possible Sh bug?

andy@syma.sussex.ac.uk (Andy Clews) (05/17/89)

Compare the following 'sh' commands on a Sequent Symmetry S81 (DYNIX
3.0.12) and a Sun4 (SunOS 4.0). Both Sh's were fired off from an
interactive Csh:

Symm. S81
---------
% sh
$ echo $?
0
$ if [ $? != 0 ]; then exit 1; fi
$ echo $?
1


Sun4
----
% 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 :-) :-)

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. 

I would welcome any help from the net before I start accusing either
Sequent or Sun.

Thanks.
-- 
Andy Clews, Computing Service, Univ. of Sussex, Brighton BN1 9QN, ENGLAND
JANET: andy@syma.sussex.ac.uk   BITNET: andy%syma.sussex.ac.uk@uk.ac
Voice: +44 273 606755 ext.2129

kamat@uceng.UC.EDU (Govind N. Kamat) (05/19/89)

In article <1002@syma.sussex.ac.uk> andy@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@uceng.UC.EDU			University of Cincinnati
					Cincinnati, OH 45221, USA

jiii@visdc.UUCP (John E Van Deusen III) (05/20/89)

Although this may not be strictly relevant to these particular machines,
according to the SysV manual entry for sh(1),

"If no *else* list or *then*" list is executed, then the *if* command
returns a zero exit status."
--
John E Van Deusen III, PO Box 9283, Boise, ID  83707, (208) 343-1865

uunet!visdc!jiii

guy@auspex.auspex.com (Guy Harris) (05/20/89)

>The status 1 after the "if" command is the one returned by the last command
>executed, in this case "test".  ("[" is a link to test - see the man page for
>test(1)).

The first isn't true in SunOS 3.0 and later; to quote from another
article:

	Although this may not be strictly relevant to these particular
	machines, according to the SysV manual entry for sh(1),

	"If no *else* list or *then*" list is executed, then the *if* command
	returns a zero exit status."

which *is* relevant to the Sun, since the Bourne shell in SunOS has been
derived from the System V version since SunOS 3.0 (R2 in 3.x, R3.1 in
4.x).

The second is true in SunOS, but irrelevant - the S5 Bourne shell has
"[" as a builtin, with "test" a synonym for it.