On 06/22/2011 06:51 AM, Dmitry Bolshakov wrote: > Hi > > set -e > if nosuchcommand | grep blabla > then > echo ERROR: blabla was found > exit 1 > fi > echo it was unexpected for me, I thougt this line should not be echoed
Not a bug; this last echo is supposed to happen. The documentation for 'set -e' is clear: Exit immediately if a pipeline (which may consist of a single simple command), a subshell command enclosed in parentheses, or one of the commands executed as part of a command list enclosed by braces (see SHELL GRAMMAR above) exits with a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test following the if or elif reserved words, part of any command executed in a && or ⎪⎪ list except the command following the final && or ⎪⎪, any command in a pipeline but the last, or if the command's return value is being inverted with !. Since the pipeline 'nosuchcommand | grep blabla' is executed as part of the test following an 'if', then 'set -e' does not abort the shell on failure. 'set -e' is a bear to use - it generally does not protect you from everything that you think it ought to, and has a number of portability bugs to boot as you migrate between versions of bash or between other shells. Not to mention that the POSIX folks can't even seem to get it right; the definition of 'set -e' had to be amended even after POSIX 2008 to match historical practice, which in turn disagreed with bash practice at the time. -- Eric Blake ebl...@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature