-e flag (Was: shell &&, || constructs)

Leo de Wit leo at philmds.UUCP
Mon Oct 3 03:39:49 AEST 1988


In article <13810 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>Regarding `set -e': again, beware!  Old versions of /bin/sh will
>exit if the test portion of an `if', `while', or `until' returns
>a nonzero status, if `-e' is set.  (These are fixed in 4.3BSD-tahoe.)

But this is according to the spec: exit *IMMEDIATELY* if a command fails.
Since the test portion is a command list, the first command of this list
that fails should force an exit.
However I agree the fix perhaps makes more sense. Maybe we need a new
metacharacter (another one!) to indicate "ignore this command's exit
status with regard to -e"; compare with make's - .

>Incidentally, this bug in the 4BSD `/bin/sh'es accounts for
>makefile lines like
>
>	-[ -d ${DESTDIR}${LIBDIR} ] || mkdir ${DESTDIR}${LIBDIR}
>
>where the `-' before the test `[' seems erroneous: it tells make to run
>sh without the -e flag, so that the mkdir can happen.  An alternate
>workaround is to use
>
>	set +e; [ -d ${DESTDIR}${LIBDIR} ] || mkdir ${DESTDIR}${LIBDIR}
>
>which has the advantage that if mkdir fails, make stops.

Playing around with -e revealed some funny things:

After a set -e, $- contains not only e but also s:

$ echo $-

$ set -e
$ echo $-
se

After typing a non-existing command, this shell exited, although it was
started as an interactive shell (explicitly setting -i didn't make any
difference). Since I had rn as a stopped job, I got:

Caught a SIGTERM--.newsrc restored

and I was logged off the system 8-) !
(Yes, Bourne shells doesn't have job control, but this was a Bourne
derivative with job control; the /bin/sh suffered however from the same
problem).

After a set +e, an e flag in $- is not discarded; it seems to be treated
as setting a positional parameter, but $- is changed (starting without
the error flag):

$ echo $-

$ set +e
$ echo $-
s
$ echo $1
+e

so +e doesn't seem to work on my system.
This happened with /bin/sh under Ultrix 2.0. Any comments?

           Leo.



More information about the Comp.unix.questions mailing list