simple function causes BASH to exit when -e in effect

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

simple function causes BASH to exit when -e in effect

Kevin Layer
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 (484 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: simple function causes BASH to exit when -e in effect

DJ Mills
On Fri, Dec 22, 2017 at 1:39 PM, Kevin Layer <[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|

Re: simple function causes BASH to exit when -e in effect

Kevin Layer
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 Mills <[hidden email]> wrote:

>
>
> On Fri, Dec 22, 2017 at 1:39 PM, Kevin Layer <[hidden email]> 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
>
Reply | Threaded
Open this post in threaded view
|

Re: simple function causes BASH to exit when -e in effect

Chet Ramey
On 12/22/17 1:56 PM, Kevin Layer wrote:

> 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.

It doesn't exit because that command fails. It exits because the simple
command that is the function call fails, since a function returns the
status of the last command exited in the function body. That's why it
doesn't fail when the last command in the body is `:'.


--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    [hidden email]    http://tiswww.cwru.edu/~chet/

Reply | Threaded
Open this post in threaded view
|

Re: simple function causes BASH to exit when -e in effect

Greg Wooledge
In reply to this post by Kevin Layer
On Fri, Dec 22, 2017 at 10:56:18AM -0800, Kevin Layer wrote:

> 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.

[ exits 1, but this doesn't trip -e because it's part of a compound
command.

However, debug1 also exits 1, and THAT trips -e, because debug1 is a
simple command.

debug2 does not exit 1 because "if" has completely different rules
compared to compound commands strung together with &&.

wooledg:~$ if false; then echo hi; fi
wooledg:~$ echo $?
0
wooledg:~$ false && echo hi; echo $?
1

Reply | Threaded
Open this post in threaded view
|

Re: simple function causes BASH to exit when -e in effect

Bob Proulx
In reply to this post by Kevin Layer
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

Reply | Threaded
Open this post in threaded view
|

Re: simple function causes BASH to exit when -e in effect

Kevin Layer
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 Proulx <[hidden email]> wrote:

> 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
>