[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
'set -e' and 'if eval false' exits with BSD sh
From: |
Ralf Wildenhues |
Subject: |
'set -e' and 'if eval false' exits with BSD sh |
Date: |
Sat, 28 Oct 2006 01:03:26 +0200 |
User-agent: |
Mutt/1.5.13 (2006-08-11) |
On FreeBSD 6.1 and OpenBSD 3.9, 'set -e' will cause failing eval
commands to exit even in an AND (&&) list or as the compound list
after 'if':
cat >errexit-eval.sh <<\EOF
#! /bin/sh
set -e
eval false && :
echo should be printed
if eval false; then :; fi
echo should be printed
EOF
sh -x errexit-eval.sh; echo $?
| + set -e
| + eval false
| + false
| 1
(similar for the second eval). It works with both
eval 'if false; then :; fi'
eval 'false && :'
OK to apply the patch below, which mentions this, updates the other
set -e example and simplifies it to use false, as suggested by Akim
on the automake-patches list?
Do you agree that this is contrary to POSIX? I can file bugs then.
Cheers,
Ralf
2006-10-27 Ralf Wildenhues <address@hidden>
* doc/autoconf.texi (Limitations of Builtins): Use 'false'
for simplicity in 'set -e' example. Suggested by Akim Demaille.
Update version information. Add 'set -e' failure on BSD shells
with a checked eval command.
Index: doc/autoconf.texi
===================================================================
RCS file: /cvsroot/autoconf/autoconf/doc/autoconf.texi,v
retrieving revision 1.1103
diff -u -r1.1103 autoconf.texi
--- doc/autoconf.texi 26 Oct 2006 16:35:15 -0000 1.1103
+++ doc/autoconf.texi 27 Oct 2006 22:49:32 -0000
@@ -12662,24 +12662,35 @@
wrapper works around the bug.
Even relatively-recent versions of the @acronym{BSD} shell (e.g.,
address@hidden 3.4) wrongly exit with @option{-e} if a command within
address@hidden 3.9) wrongly exit with @option{-e} if a command within
@samp{&&} fails inside a compound statement. For example:
@example
#! /bin/sh
set -e
-foo=''
-test -n "$foo" && exit 1
+false && exit 1
echo one
if :; then
- test -n "$foo" && exit 1
+ false && exit 1
fi
echo two
@end example
@noindent
-does not print @samp{two}. One workaround is to use @samp{if test -n
-"$foo"; then exit 1; fi} rather than @samp{test -n "$foo" && exit 1}.
+does not print @samp{two}. One workaround is to use @samp{if false; then
+exit 1; fi} rather than @samp{false && exit 1}. Also, @acronym{BSD} shells
+typically exit after a failing @command{eval}, even if it is checked:
+
address@hidden
+#! /bin/sh
+set -e
+if eval false; then :; fi
+echo should be printed
address@hidden example
+
address@hidden
+prints nothing on address@hidden 3.9 and address@hidden 6.1. One
+workaround is to put the whole @command{if} clause inside the @command{eval}.
Another possibility is to warn @acronym{BSD} users not to use @samp{sh -e}.
- 'set -e' and 'if eval false' exits with BSD sh,
Ralf Wildenhues <=