"if" statement in sh with -e flag

greep at su-dsn greep at su-dsn
Thu Sep 15 07:26:00 AEST 1983


From:  Steven Tepper <greep at 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?



More information about the Comp.unix mailing list