Re: simple function causes BASH to exit when -e in effect
Bob, 'set -e' is a double-edged sword, for sure, but I'm not sure it creates more problems than it solves. Hidden non-zero exits have wasted far more time, for me, than this little exercise has. On Sun, Dec 24, 2017 at 11:45 AM, Bob Proulxwrote: > Kevin Layer wrote: > > It took me hours of work to distill this down from a very large and long > > running script. > > > set -eu > > Hours and hours of debug time could be saved if people stopped using > 'set -e' in their programs. It creates more problems than people > think it solves. Do not meddle in the affairs of 'set -e' for it is > subtle and quick to anger. > > Bob >
Re: simple function causes BASH to exit when -e in effect
Kevin Layer wrote: > It took me hours of work to distill this down from a very large and long > running script. > set -eu Hours and hours of debug time could be saved if people stopped using 'set -e' in their programs. It creates more problems than people think it solves. Do not meddle in the affairs of 'set -e' for it is subtle and quick to anger. Bob
Re: simple function causes BASH to exit when -e in effect
The man page says: 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 !. The fact that [ exits with 1 seems to be covered by the above passage for -e. On Fri, Dec 22, 2017 at 10:50 AM, DJ Millswrote: > > > On Fri, Dec 22, 2017 at 1:39 PM, Kevin Layer wrote: > >> The bug happens to me on >> GNU bash, version 4.1.2(2)-release (x86_64-redhat-linux-gnu) >> and >> GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin16.4.0) >> >> The script is attached, but the function in question is this: >> >> function debug1 { >> [ "$debug" ] && echo "$(date "+%Y-%m-%d %H:%M:%S"): $@" >> } >> >> > This is expected behavior. When "$debug" is empty, the [ command exits 1. > That means the && isn't > run, and the whole function returns with the status of the last run > command, which is still 1 at this point. > > http://mywiki.wooledge.org/BashFAQ/105 >
Re: simple function causes BASH to exit when -e in effect
On Fri, Dec 22, 2017 at 1:39 PM, Kevin Layerwrote: > The bug happens to me on > GNU bash, version 4.1.2(2)-release (x86_64-redhat-linux-gnu) > and > GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin16.4.0) > > The script is attached, but the function in question is this: > > function debug1 { > [ "$debug" ] && echo "$(date "+%Y-%m-%d %H:%M:%S"): $@" > } > > This is expected behavior. When "$debug" is empty, the [ command exits 1. That means the && isn't run, and the whole function returns with the status of the last run command, which is still 1 at this point. http://mywiki.wooledge.org/BashFAQ/105
simple function causes BASH to exit when -e in effect
The bug happens to me on GNU bash, version 4.1.2(2)-release (x86_64-redhat-linux-gnu) and GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin16.4.0) The script is attached, but the function in question is this: function debug1 { [ "$debug" ] && echo "$(date "+%Y-%m-%d %H:%M:%S"): $@" } If it is defined like this then no problem exists: function debug1 { if [ "$debug" ]; then echo "$(date "+%Y-%m-%d %H:%M:%S"): $@" fi } nor if it is defined like this: function debug1 { [ "$debug" ] && echo "$(date "+%Y-%m-%d %H:%M:%S"): $@" : } When I run the script I see this output: BEFORE test 2 AFTER test 2 BEFORE test 1 but I expected to see BEFORE test 2 AFTER test 2 BEFORE test 1 AFTER test 1 It took me hours of work to distill this down from a very large and long running script. foo.sh is attached. foo.sh Description: Bourne shell script