Date: Tue, 27 Dec 2011 13:36:36 -0800 From: Devin Teske <devin.teske@fisglobal.com> To: "'Maxim Khitrov'" <max@mxcrypt.com>, "'FreeBSD'" <freebsd-questions@freebsd.org> Subject: RE: Unexpected sh behavior with EXIT trap and errexit Message-ID: <029f01ccc4df$9cca8190$d65f84b0$@fisglobal.com> In-Reply-To: <CAJcQMWcR9dUqv5fUifEk7r0qN55RJvTnoX2Os3NQqHaghtZ9QA@mail.gmail.com> References: <CAJcQMWcR9dUqv5fUifEk7r0qN55RJvTnoX2Os3NQqHaghtZ9QA@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
> -----Original Message-----
> From: owner-freebsd-questions@freebsd.org [mailto:owner-freebsd-
> questions@freebsd.org] On Behalf Of Maxim Khitrov
> Sent: Tuesday, December 27, 2011 12:25 PM
> To: FreeBSD
> Subject: Unexpected sh behavior with EXIT trap and errexit
>
> Can anyone explain this behavior (FreeBSD 9.0-RC3 amd64):
>
> Script:
> ----
> #!/bin/sh
>
> cleanup()
> {
> echo 'first'
> echo 'second'
> }
>
> fail() { return 42; }
>
> trap cleanup EXIT
> set -o errexit
> fail
> ----
>
> Output:
> ----
> first
> ----
>
If you change to:
fail() { false; }
Then the outcome is what you expect (both lines come out).
ASIDE: It appears that it's nothing to do with echo, stdout, or anything other than the fact that only the first command of the cleanup routine is called.
>From sh(1) regarding errexit:
"If a shell function is executed and its exit status is explicitly tested, all commands of the function are considered to be tested as well."
The exact meaning of which escapes me at the moment, but I'm lead to believe that this explanation somehow plays a role in what we're witnessing with your sample.
It may be a bug, it may not. What's interesting in the sample is that the "return 42" is a valid command that succeeds while the function itself does not.
HTH,
Devin
> Now comment out 'set -o errexit', replace 'fail' with 'fail || exit'
> (which should be equivalent to using errexit), and run again.
>
> Output:
> ----
> first
> second
> ----
>
> Am I doing something stupid or is this a bug? Bash prints out the same
> (second) output for both versions of the code.
>
> - Max
> _______________________________________________
> freebsd-questions@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org"
_____________
The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?029f01ccc4df$9cca8190$d65f84b0$>
