[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] The difference between 'if cond; then' and 'cond && {'
From: |
Peng Yu |
Subject: |
Re: [Help-bash] The difference between 'if cond; then' and 'cond && {' |
Date: |
Tue, 4 Dec 2018 08:46:02 -0600 |
On Tue, Dec 4, 2018 at 7:11 AM Greg Wooledge <address@hidden> wrote:
>
> On Mon, Dec 03, 2018 at 04:27:57PM -0700, Bob Proulx wrote:
> > Precedence in the shell is strictly left to right for && and ||.
> > Therefore one can say:
> >
> > some_cond && {
> > do something1
> > do something2
> > } || {
> > do something3
> > do something4
> > }
>
> Bad!
>
> > $ true && echo yes || echo no
> > yes
>
> BAD!
>
> https://mywiki.wooledge.org/BashPitfalls#pf22
>
> Don't do that.
I don't think this guideline is complete. If the then-branch and
else-branch guarantees to have return status 0, then `some_cond && {
do something } || { do something }` can be used according to this
guideline.
However, the real problem is that the following will return a non zero
status, whereas if-then-else always return a 0 status when "do
something" guarantees to be correct.
false && { do something }
--
Regards,
Peng