linux - Why doesn't set -e cause a failure with `false || false && true`? -
this question has answer here:
can't figure out fitting title, don't understand behavior in dash/bash. namely using set -e bail out if command fails, , command groups handle positive result.
ie. general scheme is:
[ ! wantcommand ] || command than means command gets executed if needed, , failure automatically kill script.
there might postprocessing necessary, in case use this:
[ ! wantcommand ] || { command && postprocess; } this has led curious bughunting, wont kill shell , cant behind reason. have go through chunks of shell code now, understand reason.
for testing:
bash -c 'set -e; { false || false && echo "post" ; }; echo "ec $?"' or:
bash -c 'set -e; { set -e; false || false && echo "post" ; }; echo "ec $?"' note: not asking fix, primary why returncode 1, shell wont quit
set -e bails on unchecked failures.
when branch on failure (using if, until, while, && or ||), failure checked.
if specification not written in manner, short-circuiting boolean operations not used flow control because false branches cause exit.
to quote the specification, emphasis added:
when option on, when command fails (for of reasons listed in consequences of shell errors or returning exit status greater zero), shell shall exit, if executing exit special built-in utility no arguments, following exceptions:
the failure of individual command in multi-command pipeline shall not cause shell exit. failure of pipeline shall considered.
the -e setting shall ignored when executing compound list following
while,until,if, orelifreserved word, pipeline beginning!reserved word, or any command of and-or list other last.if exit status of compound command other subshell command result of failure while
-ebeing ignored,-eshall not apply command.this requirement applies shell environment , each subshell environment separately. example, in:
set -e; (false; echo one) | cat; echo 2the
falsecommand causes subshell exit without executingecho one; however,echo twoexecuted because exit status of pipeline(false; echo one) | catzero.
note specification has changed on time; shells implementing prior revision of posix specification may not precisely comply version quoted here.
to inject opinion here -- i'd suggest reading bashfaq #105 , ensuring understand behaviors described therein before making decision use set -e rather implementing explicit error-handling hand. fvue wiki further describes distinctions in behavior between set -e in bash-native mode , posix mode, should likewise understood.
Comments
Post a Comment