Article View: comp.unix.shell
Article #3992Re: Exit status of "if" statement?
From: Janis Papanagnou
Date: Mon, 09 Apr 2012 18:40
Date: Mon, 09 Apr 2012 18:40
41 lines
2589 bytes
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!
>
>
Technical Details
Message-ID:
<jlvaeg$3g4$1@news.m-online.net>Path:
arc1-ca.pugleaf.net!r9ni37450pbh.0!nntp.google.com!news1.google.com!volia.net!news2.volia.net!feed-A.news.volia.net!news.ecp.fr!feeder3.cambriumusenet.nl!feed.tweaknews.nl!195.62.100.242.MISMATCH!newsfeed.kamp.net!newsfeed.kamp.net!newspeer1.de.telia.net!newspeer4.de.telia.net!de.telia.net!news.m-online.net!.POSTED!not-for-mailReferences:
<3360b939-5d82-4258-90a2-58c2d5545943@pg2g2000pbb.googlegroups.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10038b,9538181a045179a4
X-Google-NewGroupId: yes
X-Google-Attributes: giddbda8d187b,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Received: by 10.68.236.170 with SMTP id uv10mr7919963pbc.4.1333996819325;
Mon, 09 Apr 2012 11:40:19 -0700 (PDT)
Path: arc1-ca.pugleaf.net!r9ni37450pbh.0!nntp.google.com!news1.google.com!volia.net!news2.volia.net!feed-A.news.volia.net!news.ecp.fr!feeder3.cambriumusenet.nl!feed.tweaknews.nl!195.62.100.242.MISMATCH!newsfeed.kamp.net!newsfeed.kamp.net!newspeer1.de.telia.net!newspeer4.de.telia.net!de.telia.net!news.m-online.net!.POSTED!not-for-mail
From: Janis Papanagnou <janis_papanagnou@hotmail.com>
Newsgroups: comp.unix.shell,gnu.bash.bug
Subject: Re: Exit status of "if" statement?
Date: Mon, 09 Apr 2012 20:40:15 +0200
Organization: (posted via) M-net Telekommunikations GmbH
Lines: 42
Message-ID: <jlvaeg$3g4$1@news.m-online.net>
References: <3360b939-5d82-4258-90a2-58c2d5545943@pg2g2000pbb.googlegroups.com>
NNTP-Posting-Host: ppp-188-174-102-72.dynamic.mnet-online.de
Mime-Version: 1.0
X-Trace: news.m-online.net 1333996816 3588 188.174.102.72 (9 Apr 2012 18:40:16 GMT)
X-Complaints-To: news@news.m-online.net
NNTP-Posting-Date: Mon, 9 Apr 2012 18:40:16 +0000 (UTC)
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.9.2.28) Gecko/20120313 Thunderbird/3.1.20
In-Reply-To: <3360b939-5d82-4258-90a2-58c2d5545943@pg2g2000pbb.googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit