bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: set -e, braces and compound commands


From: Chet Ramey
Subject: Re: set -e, braces and compound commands
Date: Fri, 11 Feb 2011 10:03:20 -0500
User-agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.13) Gecko/20101207 Thunderbird/3.1.7

On 2/10/11 11:08 PM, Daniel Villeneuve wrote:
> I'm using GNU bash, version 4.1.2(1)-release (i386-redhat-linux-gnu).
> 
> Following the change of semantics of "set -e" in bash 4,"man bash" seems to
> imply that the following script should exit after the for command (from the
> text "or one of the commands executed as part of a command list enclosed
> by  braces" where "one of the commands" should match a "for" command) . 
> However it does not.
> 
> 
> set -e
> {
>   for i in a b c; do
>     [ -z "$i" ] && echo "null"
>   done
>   echo "'for' return code = $?"
> }
> echo "brace return code = $?"
> 
> 
> For reference, I've consulted
> http://thread.gmane.org/gmane.comp.standards.posix.austin.general/282,
> http://thread.gmane.org/gmane.comp.shells.bash.bugs/13465 and
> the current public POSIX spec I have access to which still says:
> 
> *-e*
>    When this option is on, if a simple command fails for any of the
>    reasons listed in Consequences of Shell Errors
>    <ref to V3_chap02.html#tag_18_08_01>
>    or returns an exit status value >0, and is not part of the compound
>    list following a *while*, *until*, or *if* keyword, and is not a
>    part of an AND or OR list, and is not a pipeline preceded by the *!*
>    reserved word, then the shell shall immediately exit.
> 
> 
> Question 1: Is it the case that the bash 4 behavior is as intended here and
> that it's the documention of "set -e" in bash 4 that is imprecise
> ("commands" being too broad)?

I think the key is that the (new and as yet unpublished) Posix description
includes this sentence:

         Additionally if the
         failed command is a compound command whose exit status was
         derived from a command that is one of the aforementioned
         exceptions, the shell shall not exit.

That appears to cover this case.

> 
> Also, note that the man page section on "trap ERR" says that the trap is
> triggered if a "simple command" returns >0 (sharing exceptions with "set
> -e").  It seems however that the trigger is the same as for "set -e", as in
> 
> trap 'echo "error caught ($?), exiting"; exit 1' ERR
> (exit 2)
> echo done
> 
> Question 2: Are "set -e" and "trap ... ERR" triggered by the same events?

Yes, that's the intent.

Chet

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]