functions and set -e
Hi Here's a test case which demonstrates the problem: #!/bin/sh set -e func () { false && echo false true && echo true false && echo false } func echo done It never echoes "done" because func() returns 1. This seems to go against what the bash manual says about "set -e" "Exit immediately if a simple command (*note Simple Commands::) exits with a non-zero status, unless the command that fails is part of the command list immediately following a `while' or `until' keyword, part of the test in an `if' statement, part of a `&&' or `||' list," The problem appears to be specific to functions. Is this a bug? Thanks Greg ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: functions and set -e
Greg Schafer <[EMAIL PROTECTED]> wrote: > #!/bin/sh > set -e > > func () { > false && echo false > true && echo true > false && echo false > } > > func > > echo done > > > It never echoes "done" because func() returns 1. That's the correct behavior. The last "false" within the function does not immediately cause bash to exit, since it is part of the "&&" comound statement. But then the function call itself, which is a simple command in its own right, has a nonzero exit status, so bash exits at that point. paul ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: functions and set -e
On Wed, Aug 30, 2006 at 12:03:51AM -0400, Paul Jarc wrote: > Greg Schafer <[EMAIL PROTECTED]> wrote: > > #!/bin/sh > > set -e > > > > func () { > > false && echo false > > true && echo true > > false && echo false > > } > > > > func > > > > echo done > > > > > > It never echoes "done" because func() returns 1. > > That's the correct behavior. The last "false" within the function > does not immediately cause bash to exit, since it is part of the "&&" > comound statement. But then the function call itself, which is a > simple command in its own right, has a nonzero exit status, so bash > exits at that point. I'll take your word for it.. but I'm not totally convinced. At the very least, this behavior is very confusing and apparently not documented. It's just plain weird that the compound statement containing "false" causes the function call to end up with a nonzero exit status only bacause the statement appears *on the last line* of the function. The exact same statement on the first line of the function behaves as expected. Regards Greg ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: functions and set -e
> > > #!/bin/sh > > > set -e > > > > > > func () { > > > false && echo false > > > true && echo true > > > false && echo false ^^^ Line 1 > > > } > > > > > > func ^^^ Line 2 > > > > > > echo done > > > > I'll take your word for it.. but I'm not totally convinced. At the very > least, this behavior is very confusing and apparently not documented. It's > just plain weird that the compound statement containing "false" causes the > function call to end up with a nonzero exit status only bacause the > statement appears *on the last line* of the function. The exact same > statement on the first line of the function behaves as expected. As marked above, the line causing bash to exit is not line 1 (which was a compound statement), but line 2 (invoking a function by a simple statement). Try rewriting line 2 as "func && echo false" to see the difference. -- Eric Blake ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: functions and set -e
On Wed, Aug 30, 2006 at 04:28:35AM +, Eric Blake wrote: > > > > #!/bin/sh > > > > set -e > > > > > > > > func () { > > > > false && echo false > > > > true && echo true > > > > false && echo false > ^^^ Line 1 > > > > > } > > > > > > > > func > ^^^ Line 2 > > > > > > > > > echo done > > > > > > I'll take your word for it.. but I'm not totally convinced. At the very > > least, this behavior is very confusing and apparently not documented. It's > > just plain weird that the compound statement containing "false" causes the > > function call to end up with a nonzero exit status only bacause the > > statement appears *on the last line* of the function. The exact same > > statement on the first line of the function behaves as expected. > > As marked above, the line causing bash to exit is not line 1 (which > was a compound statement), but line 2 (invoking a function by a > simple statement). Try rewriting line 2 as "func && echo false" to > see the difference. Thanks for trying to clarify it for me. Let me put it another way: If I change Line 1 above to an if/then style statement instead of "&&" ie: if false; then echo false; fi it works exactly like I'd expect instead of the counter-intuitive behavior when using &&. I still suspect something is amiss here. Maybe the bash docs just need to be clarified.. Anyhoo, I suppose I should have tested with other shells before reporting but I don't have any available at the moment.. Regards Greg ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: functions and set -e
Greg Schafer <[EMAIL PROTECTED]> wrote: > Thanks for trying to clarify it for me. Let me put it another way: If I > change Line 1 above to an if/then style statement instead of "&&" ie: > > if false; then echo false; fi > > it works exactly like I'd expect instead of the counter-intuitive behavior > when using &&. That's because the exit status if an "if" command with a false condition and no "else" clause is 0, while the status of the "&&" command is not. bash is behaving exactly as the documentation says: the function call itself is a simple command, so if it returns nonzero, bash exits. The internal structure of the function is irrelevant. What matters is the exit status of the simple command, not how that status is produced from other, possibly non-simple commands. If you want the function to never cause bash to exit, insert an extra "true" at the end. paul ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: functions and set -e
On Wed, Aug 30, 2006 at 01:01:06AM -0400, Paul Jarc wrote: > Greg Schafer <[EMAIL PROTECTED]> wrote: > > Thanks for trying to clarify it for me. Let me put it another way: If I > > change Line 1 above to an if/then style statement instead of "&&" ie: > > > > if false; then echo false; fi > > > > it works exactly like I'd expect instead of the counter-intuitive behavior > > when using &&. > > That's because the exit status if an "if" command with a false > condition and no "else" clause is 0, while the status of the "&&" > command is not. > > bash is behaving exactly as the documentation says: the function call > itself is a simple command, so if it returns nonzero, bash exits. That is the crux of the problem. I was expecting the function to return zero. But as you quite rightly say, the exit status of the "&&" command in this instance is nonzero and this is the key point I was missing. It's covered in the "Lists of Commands" section of the manual: "The return status of AND and OR lists is the exit status of the last command executed in the list." Ok, sorted. Thanks for your patience. I'm gonna put this down as a trap for young players :-) Regards Greg ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash