triggering of ERR trap differs between bash 3.2.39 & 4.0.28

View: New views
6 Messages — Rating Filter:   Alert me  

triggering of ERR trap differs between bash 3.2.39 & 4.0.28

by Amit Dor-Shifer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I've the following script:

set -eE
on_error() { echo ERROR ERROR; }
trap on_error ERR

if [ -n "$(echo $1 |grep a)" ];then
   echo "input contains 'a'"
fi

When executed under bash-4, on_error is evaluated as part of the
expression in the 'if'. This does not happen in 3.2:


<bash-3>
amit0 ~ # bash --version
GNU bash, version 4.0.28(2)-release (x86_64-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
amit0 ~ # bash -xc 'set -eE;on_error() { echo ERROR ERROR; };trap
on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo "input contains
'a'";fi' a
+ set -eE
+ trap on_error ERR
++ grep a
++ echo a
+ '[' -n a ']'
+ echo 'input contains a'
input contains a
amit0 ~ # bash -xc 'set -eE;on_error() { echo ERROR ERROR; };trap
on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo "input contains
'a'";fi' b
+ set -eE
+ trap on_error ERR
++ echo b
++ grep a
+ '[' -n '' ']'
</bash -3>

<bash-4>
cm.stargate ~ # bash -xc 'set -eE;on_error() { echo ERROR ERROR; };trap
on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo "input contains
'a'";fi' a
+ set -eE
+ trap on_error ERR
++ echo a
++ grep a
+ '[' -n a ']'
+ echo 'input contains a'
input contains a
cm.stargate ~ # bash -xc 'set -eE;on_error() { echo ERROR ERROR; };trap
on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo "input contains
'a'";fi' b
+ set -eE
+ trap on_error ERR
++ echo b
++ grep a
+++ on_error
+++ echo ERROR ERROR
+ '[' -n 'ERROR ERROR' ']'
+ echo 'input contains a'
input contains a
</bash-4>

I'm confused: I assumed that the culprit clause complies as "part of the
test in an if statement" (man bash) and hence would not trigger an ERR
signal.

And then there's the difference between bashes: I couldn't find
reference to this in CHANGES.

Would appreciate comments and more references.

10x,
Amit





Parent Message unknown Re: triggering of ERR trap differs between bash 3.2.39 & 4.0.28

by pk-16 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Amit Dor-Shifer wrote:

> I've the following script:
>
> set -eE
> on_error() { echo ERROR ERROR; }
> trap on_error ERR
>
> if [ -n "$(echo $1 |grep a)" ];then
>    echo "input contains 'a'"
> fi
>
> When executed under bash-4, on_error is evaluated as part of the
> expression in the 'if'. This does not happen in 3.2:

I think this is part of the new behavior of -e in bash 4. See for example
here:

http://www.mail-archive.com/bug-bash@.../msg06288.html


Re: triggering of ERR trap differs between bash 3.2.39 & 4.0.28

by Chet Ramey :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Amit Dor-Shifer wrote:

> cm.stargate ~ # bash -xc 'set -eE;on_error() { echo ERROR ERROR; };trap
> on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo "input contains
> 'a'";fi' b
> + set -eE
> + trap on_error ERR
> ++ echo b
> ++ grep a
> +++ on_error
> +++ echo ERROR ERROR
> + '[' -n 'ERROR ERROR' ']'
> + echo 'input contains a'
> input contains a
> </bash-4>

I can't reproduce it with bash-4.0.35:

$ ../bash-4.0-patched/bash -xc 'set -eE;on_error() { echo ERROR ERROR;
};trap on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo "input
contains 'a'";fi' b
+ set -eE
+ trap on_error ERR
++ echo b
++ grep a
+ '[' -n '' ']'
$ ../bash-4.0-patched/bash --version
GNU bash, version 4.0.35(9)-release (i386-apple-darwin10.0.0)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    chet@...    http://cnswww.cns.cwru.edu/~chet/



Re: triggering of ERR trap differs between bash 3.2.39 & 4.0.28

by Amit Dor-Shifer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ok. Same here. (4.0.35 is not yet stable on my distro, gentoo, hence I
quoted results with 4.0.28).

I'm wondering: CHANGES doesn't refer to any bug numbers or anything.
Isn't bash using some issue-tracking framework?
I suppose this issue falls under:
<quote>
l.  Changed behavior of shell when -e option is in effect to reflect
consensus
    of Posix shell standardization working group.
</quote>

Thanks,
Amit



Chet Ramey wrote:

> Amit Dor-Shifer wrote:
>
>  
>> cm.stargate ~ # bash -xc 'set -eE;on_error() { echo ERROR ERROR; };trap
>> on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo "input contains
>> 'a'";fi' b
>> + set -eE
>> + trap on_error ERR
>> ++ echo b
>> ++ grep a
>> +++ on_error
>> +++ echo ERROR ERROR
>> + '[' -n 'ERROR ERROR' ']'
>> + echo 'input contains a'
>> input contains a
>> </bash-4>
>>    
>
> I can't reproduce it with bash-4.0.35:
>
> $ ../bash-4.0-patched/bash -xc 'set -eE;on_error() { echo ERROR ERROR;
> };trap on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo "input
> contains 'a'";fi' b
> + set -eE
> + trap on_error ERR
> ++ echo b
> ++ grep a
> + '[' -n '' ']'
> $ ../bash-4.0-patched/bash --version
> GNU bash, version 4.0.35(9)-release (i386-apple-darwin10.0.0)
> Copyright (C) 2009 Free Software Foundation, Inc.
> License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
>
> This is free software; you are free to change and redistribute it.
> There is NO WARRANTY, to the extent permitted by law.
>
>  



Re: triggering of ERR trap differs between bash 3.2.39 & 4.0.28

by Chet Ramey :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Ok. Same here. (4.0.35 is not yet stable on my distro, gentoo, hence I
> quoted results with 4.0.28).

But I can't reproduce it with bash-4.0.28, either:

$ ./bash --version
GNU bash, version 4.0.28(2)-release (i386-apple-darwin9.8.0)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ ./bash -xc 'set -eE;on_error() { echo ERROR ERROR; };trap on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo "input contains 'a'";fi' a
+ set -eE
+ trap on_error ERR
++ echo a
++ grep a
+ '[' -n a ']'
+ echo 'input contains a'
input contains a
$ ./bash -xc 'set -eE;on_error() { echo ERROR ERROR; };trap on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo "input contains 'a'";fi' b
+ set -eE
+ trap on_error ERR
++ echo b
++ grep a
+ '[' -n '' ']'


--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    chet@...    http://cnswww.cns.cwru.edu/~chet/



Re: triggering of ERR trap differs between bash 3.2.39 & 4.0.28

by Amit Dor-Shifer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Seems like I've erroneously switched between bash versions exhibiting
the difference. It's 3.2.39 that evaluates 'on_error' when catching the
trap.

 From the start:

# bash-4.0_p28/image/bin/bash --version
GNU bash, version 4.0.28(2)-release (x86_64-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
# bash-4.0_p28/image/bin/bash -xc 'set -eE;on_error() { echo ERROR
ERROR; };trap on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo
"input contains 'a'";fi' b
+ set -eE
+ trap on_error ERR
++ echo b
++ grep a
+ '[' -n '' ']'
# bash-3.2_p39/image/bin/bash --version
GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
# bash-3.2_p39/image/bin/bash -xc 'set -eE;on_error() { echo ERROR
ERROR; };trap on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo
"input contains 'a'";fi' b
+ set -eE
+ trap on_error ERR
++ echo b
++ grep a
+++ on_error
+++ echo ERROR ERROR
+ '[' -n 'ERROR ERROR' ']'
+ echo 'input contains a'
input contains a

Amit

Chet Ramey wrote:

>> Ok. Same here. (4.0.35 is not yet stable on my distro, gentoo, hence I
>> quoted results with 4.0.28).
>>    
>
> But I can't reproduce it with bash-4.0.28, either:
>
> $ ./bash --version
> GNU bash, version 4.0.28(2)-release (i386-apple-darwin9.8.0)
> Copyright (C) 2009 Free Software Foundation, Inc.
> License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
>
> This is free software; you are free to change and redistribute it.
> There is NO WARRANTY, to the extent permitted by law.
> $ ./bash -xc 'set -eE;on_error() { echo ERROR ERROR; };trap on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo "input contains 'a'";fi' a
> + set -eE
> + trap on_error ERR
> ++ echo a
> ++ grep a
> + '[' -n a ']'
> + echo 'input contains a'
> input contains a
> $ ./bash -xc 'set -eE;on_error() { echo ERROR ERROR; };trap on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo "input contains 'a'";fi' b
> + set -eE
> + trap on_error ERR
> ++ echo b
> ++ grep a
> + '[' -n '' ']'
>
>
>  


Chet Ramey wrote:

>> Ok. Same here. (4.0.35 is not yet stable on my distro, gentoo, hence I
>> quoted results with 4.0.28).
>>    
>
> But I can't reproduce it with bash-4.0.28, either:
>
> $ ./bash --version
> GNU bash, version 4.0.28(2)-release (i386-apple-darwin9.8.0)
> Copyright (C) 2009 Free Software Foundation, Inc.
> License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
>
> This is free software; you are free to change and redistribute it.
> There is NO WARRANTY, to the extent permitted by law.
> $ ./bash -xc 'set -eE;on_error() { echo ERROR ERROR; };trap on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo "input contains 'a'";fi' a
> + set -eE
> + trap on_error ERR
> ++ echo a
> ++ grep a
> + '[' -n a ']'
> + echo 'input contains a'
> input contains a
> $ ./bash -xc 'set -eE;on_error() { echo ERROR ERROR; };trap on_error ERR;if [ -n "$(echo $0 |grep a)" ];then echo "input contains 'a'";fi' b
> + set -eE
> + trap on_error ERR
> ++ echo b
> ++ grep a
> + '[' -n '' ']'
>
>
>