set -e breaks some shell scripts

Earl H. Kinmonth ehkinmonth at ucdavis.edu
Tue Aug 1 07:46:32 AEST 1989


Presumably what I'm going to describe is common knowledge for adepts,
but for the benefit of a few other amateurs out there, I'm sharing
my information (and frustrations).

Situation: a shell script that runs under a half dozen varieties of
the Bourne shell (and even the Korn shell under MSDOS) dies without
explanation on a particular set of machines.  In each case the death
without explanation occurs in a construct of the form

if test -r $F
then
echo found $F
fi

Using the set -x option shows that test is being executed AND that
the shell is instantly exiting.

After hours of frustration, switching back to other machines to verify
that the code actually works, I see a "set -e" in the shell script.
I comment it out, run the shell script, and it works!

Analysis: for archaic versions of the Bourne shell, test is a separate
program.  If the -e option is on, a non-zero status is considered an
execution failure, and the shell exits (unfortunately with no
message).  With more recent versions of /bin/sh and ksh, test is a
built in function, and a non-zero status is recognized as a normal
state of affairs and execution continues.

Save your RTFM comments.  I did RTFM, and if this problem is
explained, I missed it.



More information about the Comp.unix mailing list