[net.unix] "if" statement in sh with -e flag

greep@su-dsn@sri-unix.UUCP (09/15/83)

From:  Steven Tepper <greep@su-dsn>

The "if" statement in /bin/sh seems to be pretty useless if the shell
is running with the -e flag (which says to exit on any errors, including
non-zero returns from programs).  The problem is that if the program
called as the test returns non-zero, the shell exits instead of doing
the "else" part.  Of course if you know it will always return 0, then
there's no point to the test.  I found this when trying to use it in
a makefile, since Make calls sh with -e, and there doesn't seem to be
any way to "unset" the -e flag.  I got around it by calling another sh
(without -e), eg:

newfile:
	sh -c \
	    "if cmp foo1 foo2; \
		then \
		     echo 'File unchanged'; \
		else \
		     set -e; \
		     bar; \
		fi"

(the "set -e" is so Make will stop if bar dies).  Does anyone know of
a better way to handle this?

gwyn@brl-vld@sri-unix.UUCP (09/15/83)

From:      Doug Gwyn (VLD/VMB) <gwyn@brl-vld>

The reported behavior does not occur with the UNIX System V Bourne shell.

dce@tekecs.UUCP (David Elliott) (09/24/83)

The example you gave does not need the 'set -e'. Make will stop if the
exit code returned by the "sh -c" is non-zero. In this case, if bar
fails, the exit code will be nonzero and Make will stop with the message :

	** Error code 1
	Stop.
	
			David