Article View: comp.unix.shell
Article #3994Re: Exit status of "if" statement?
From: Barry Margolin
Date: Tue, 10 Apr 2012 06:26
Date: Tue, 10 Apr 2012 06:26
67 lines
3463 bytes
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 ***
Technical Details
Message-ID:
<barmar-993346.02262410042012@news.eternal-september.org>Path:
arc1-ca.pugleaf.net!h15ni106653bkw.0!nntp.google.com!news2.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mailReferences:
<3360b939-5d82-4258-90a2-58c2d5545943@pg2g2000pbb.googlegroups.com> <jlvaeg$3g4$1@news.m-online.net> <dd6183e6-29e2-4f7a-865d-0e085ca9f156@ri8g2000pbc.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
Received: by 10.205.119.5 with SMTP id fs5mr965187bkc.7.1334039185019;
Mon, 09 Apr 2012 23:26:25 -0700 (PDT)
MIME-Version: 1.0
Path: arc1-ca.pugleaf.net!h15ni106653bkw.0!nntp.google.com!news2.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Barry Margolin <barmar@alum.mit.edu>
Newsgroups: comp.unix.shell,gnu.bash.bug
Subject: Re: Exit status of "if" statement?
Date: Tue, 10 Apr 2012 02:26:24 -0400
Organization: A noiseless patient Spider
Lines: 67
Message-ID: <barmar-993346.02262410042012@news.eternal-september.org>
References: <3360b939-5d82-4258-90a2-58c2d5545943@pg2g2000pbb.googlegroups.com> <jlvaeg$3g4$1@news.m-online.net> <dd6183e6-29e2-4f7a-865d-0e085ca9f156@ri8g2000pbc.googlegroups.com>
Injection-Info: barmar.motzarella.org; posting-host="ePtxJaRXJPFeIWBMlKfZaA";
logging-data="14761"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18EhbkdAf2W4d/ffE4DppyE"
User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)
Cancel-Lock: sha1:HEgNS0b+AzLAUcbNQbQF02d8oJc=