[comp.sys.sequent] 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

pwolfe@kailand.KAI.COM (05/19/89)

> /* Written by andy@syma.sussex.ac.uk in kailand:comp.sys.sequent */
> 
> Symm. S81
> ---------
> % sh
> $ echo $?
> 0
> $ if [ $? != 0 ]; then exit 1; fi
> $ echo $?
> 1

First off, you are using the string comparison (!=) to compare numbers.  You
should be using "-ne".  See the manpage for test(1).

> What exit status (value) should the "if" return?

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)).  You can demonstrate this by:

	$ echo $?
	$ if [ $? -eq 0 ]; then echo yes; fi
	yes
	$ echo $?
	0
	$ if [ $? -ne 0 ]; then echo no; fi
	$ echo $?
	1

Since $? is set so quickly, you'd be better off by assigning it to a variable,
and testing and displaying that variable.

I have no explanation why Sun's test returned a status of zero.  It might be
because of the character comparison.

Patrick Wolfe	(pat@kai.com, {uunet,uiucuxc,sequent}!kailand!pat)
System Manager, Kuck and Associates, Inc.

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.

pwolfe@kailand.KAI.COM (05/21/89)

/* Written by guy@auspex.auspex.com in kailand:comp.sys.sequent */
> the Bourne shell in SunOS has been derived from the System V version
> the S5 Bourne shell has "[" as a builtin, with "test" a synonym for it.

On Sequents, the Bourne shell in the BSD universe comes from Berzerkeley, and
does not have test builtin.  However, the Bourne shell in the ATT universe
is derived from Sys V.2, and does have test builtin.  It returns the same
exit codes as the Sun.

Apparently this is just one of the ways that they differ.


Patrick Wolfe	(pat@kai.com, {uunet,uiucuxc,sequent}!kailand!pat)
System Manager, Kuck and Associates, Inc.