-e does not take effects in subshell

2015-08-11 Thread PRC
I have a export function looking like:

mybuild()
{
(
set -e
make
echo "build okay"
)
}

I wish to use this function this way:

mybuild && do_other_stuff

But whatever (success or failure) make returns "build okay" is always printed 
and do_other_stuff always gets executed, which is my expectation.

I have found below descriptions on bash manual.

  -e  Exit  immediately  if  a pipeline (which may consist of a 
single simple command),  a subshell command enclosed in parentheses, or one of 
the
  commands executed as part of a command list enclosed by 
braces (see SHELL GRAMMAR above) exits with a non-zero status.  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 !.  A trap on ERR, if set, is executed before the 
shell
  exits.  This option applies to the shell environment and 
each subshell environment separately (see COMMAND EXECUTION ENVIRONMENT above), 
and
  may cause subshells to exit before executing all the 
commands in the subshell.

To my understanding, the purpose of these limitations is to prevent the shell 
exiting if a command fails in conditional testing context such as if, elif, 
while, isn't it?
My confusion is why the && operator disables `-e' effects in the subshell. It 
does not make sense to continue executing remaining commands since I wish the 
subshell to stop on error.

Or I am wrong and the results match the original designs?

BR
  

RE: -e does not take effects in subshell

2015-08-12 Thread PRC
1.  If the function is complicated and contains lots of commands, it 
would be more troublesome appending && to every command rather 
than simply using one single "set -e" at the front.
2.  It is not 
reasonable that suppression of '-e' is applied as well inside the 
function. My intention is if my_build succeeds continue to do other 
stuff. Does anyone like the rule that a function never fails only if it is part 
of a 
compound command?

> Date: Tue, 11 Aug 2015 09:50:56 -0400
> From: wool...@eeg.ccf.org
> To: ijk...@msn.com
> CC: bug-bash@gnu.org
> Subject: Re: -e does not take effects in subshell
> 
> On Tue, Aug 11, 2015 at 11:42:29AM +, PRC wrote:
> > mybuild()
> > {
> > (
> > set -e
> > make
> > echo "build okay"
> > )
> > }
> > 
> > mybuild && do_other_stuff
> 
> http://mywiki.wooledge.org/BashFAQ/105
> 
> Since mybuild is invoked as part of a compound command, set -e is
> suppressed.  I guess this applies not only to set -e that's invoked
> beforehand, but even to set -e that's set within the compound command.
> 
> Stop using set -e and all of these problems simply go away.
> 
> mybuild() {
> make && echo "build okay"
> }
> 
> mybuild && do_other_stuff