[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Exit status of "if" statement?
From: |
Janis Papanagnou |
Subject: |
Re: Exit status of "if" statement? |
Date: |
Mon, 09 Apr 2012 20:40:15 +0200 |
User-agent: |
Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.9.2.28) Gecko/20120313 Thunderbird/3.1.20 |
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!
>
>