On 09.04.2012 19:31, Dan Stromberg wrote:
> 
> What should be the behavior of the following?
> 
> if cmd1
> then
>     cmd2
> fi && if cmd3
> then
>    cmd4
> fi

If cmd1 is true then execute cmd2;
  cmd2 defines the exit code for the first if
depending on cmd2 return value,
  if true then the subsequent if is executed
    if cmd3 is true then execute cmd4;
      cmd4 defines the exit code for the second if

> 
> I've not joined two if's with a short-circuit boolean before, but I'm
> suddenly working on a script where someone else has.
> 
> Playing around, it appears that cmd1 and cmd3 have no direct impact on
> the exit codes of the two if's, while cmd2 and cmd4 do (if cmd1 or
> cmd3 evaluate true).

Yes. cmd1 and cmd3 control the if condition, and the resulting exit code
is defined by the last command executed, either cmd2 or cmd4.

>  Is this the defined behavior in POSIX shell?  In
> bash?  In bash symlinked to /bin/sh?  In dash?

I think it is defined that way in all POSIX complient shells.

Janis

> 
> TIA!
> 
> 

Reply via email to