🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.unix.shell
4 messages
4 total messages Started by Dan Stromberg Mon, 09 Apr 2012 17:31
Exit status of "if" statement?
#3991
Author: Dan Stromberg
Date: Mon, 09 Apr 2012 17:31
20 lines
1895 bytes
What should be the behavior of the following?

if cmd1
then
    cmd2
fi && if cmd3
then
   cmd4
fi

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).  Is this the defined behavior in POSIX shell?  In
bash?  In bash symlinked to /bin/sh?  In dash?

TIA!
Re: Exit status of "if" statement?
#3992
Author: Janis Papanagnou
Date: Mon, 09 Apr 2012 18:40
41 lines
2589 bytes
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!
> 
> 
Re: Exit status of "if" statement?
#3993
Author: bsh
Date: Tue, 10 Apr 2012 02:26
72 lines
3816 bytes
Janis Papanagnou <janis_papanag...@hotmail.com> wrote:
> Dan Stromberg wrote:
> > What should be the behavior of the following?
> > if cmd1
> > then
> >     cmd2
> > fi && if cmd3
> > then
> >    cmd4
> > fi

Hello Daniel and Janis!

> 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 see a problem, which I cannot immediate test on a
command line available to me now.

First of all, the manpage plainly indicates:

"Usage: if if-list;then list[;elif list;then list]... [;else list];fi
... If the if-list has a non-zero exit status and there is
no else-list, then the if command returns a zero exit status."

> > 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.

... And because of this, it is impossible to discern whether
the return code is the result of a failed if-list or the
last command in the if-body code. This strikes me as poor
programming discipline.

BTW, the reason that the manpage keeps talking about command
"lists", instead of individual commands, is because the "if"
criterion is the return code of the _last_ command in a command
_list_. That is to say, the following is possible and even
recommended:

if    cmd1
      cmd2
      cmd3
then  ...
fi

It is the return code of cmd3 which determines the flow-of-
control. This construction reinforces readable and structured
localization of code.

> > 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.

Yes:

POSIX XSH: (2.9.4) Compound Commands: The if Conditional Construct:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_04

"The exit status of the 'if' command shall be the exit status of
the 'then' or 'else' compound-list that was executed, or zero,
if none was executed."

=Brian
Re: Exit status of "if" statement?
#3994
Author: Barry Margolin
Date: Tue, 10 Apr 2012 06:26
67 lines
3463 bytes
In article 
<dd6183e6-29e2-4f7a-865d-0e085ca9f156@ri8g2000pbc.googlegroups.com>,
 bsh <brian_hiles@rocketmail.com> wrote:

> Janis Papanagnou <janis_papanag...@hotmail.com> wrote:
> > Dan Stromberg wrote:
> > > What should be the behavior of the following?
> > > if cmd1
> > > then
> > > � � cmd2
> > > fi && if cmd3
> > > then
> > > � �cmd4
> > > fi
> 
> Hello Daniel and Janis!
> 
> > 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 see a problem, which I cannot immediate test on a
> command line available to me now.
> 
> First of all, the manpage plainly indicates:
> 
> "Usage: if if-list;then list[;elif list;then list]... [;else list];fi
> ... If the if-list has a non-zero exit status and there is
> no else-list, then the if command returns a zero exit status."
> 
> > > 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.
> 
> ... And because of this, it is impossible to discern whether
> the return code is the result of a failed if-list or the
> last command in the if-body code. This strikes me as poor
> programming discipline.

If you care about the exit status of the if-list, run it separately and 
save it.  I.e. instead of:

if cmd1; cmd2; ...
then ...
fi

write:

cmd1; cmd2; ...
status=$?
if [ $status = 0 ]
then ...
fi

The assumption behind the design of "if" is that you only care about the 
success or failure of the if-list, not the specific kind of failure.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads