Discussion:
The difference between 'if cond; then' and 'cond && {'
(too old to reply)
Peng Yu
2018-12-03 03:40:42 UTC
Permalink
It sounds like the following construct would always produce the same
result. Is it so?

if some_cond; then
do something1
do something2
fi

somc_cond && {
do something1
do something2
}
--
Regards,
Peng
Eduardo Bustamante
2018-12-03 03:52:26 UTC
Permalink
Post by Peng Yu
It sounds like the following construct would always produce the same
result. Is it so?
if some_cond; then
do something1
do something2
fi
somc_cond && {
do something1
do something2
}
There is a difference if you want to

- use "else"
- use a more complicated condition
Peng Yu
2018-12-03 04:01:44 UTC
Permalink
Post by Eduardo Bustamante
Post by Peng Yu
It sounds like the following construct would always produce the same
result. Is it so?
if some_cond; then
do something1
do something2
fi
somc_cond && {
do something1
do something2
}
There is a difference if you want to
- use "else"
I assume there is no "else".
Post by Eduardo Bustamante
- use a more complicated condition
Could you give an example when they can not be made to return the same
result no matter what? Thanks.
--
Regards,
Peng
Bob Proulx
2018-12-03 23:27:57 UTC
Permalink
Post by Peng Yu
Post by Eduardo Bustamante
Post by Peng Yu
It sounds like the following construct would always produce the same
result. Is it so?
if some_cond; then
do something1
do something2
fi
somc_cond && {
do something1
do something2
}
There is a difference if you want to
Could you give an example when they can not be made to return the same
result no matter what? Thanks.
There are interactions with 'set -e'. But "as everyone who is anyone
knows" (proof by intimidation) one should never use 'set -e'. :-)

If you need to ask then you shouldn't be doing it.
Post by Peng Yu
Post by Eduardo Bustamante
- use "else"
I assume there is no "else".
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
}

$ true && echo yes || echo no
yes

$ false && echo yes || echo no
no

But anything more complicated than a one-liner and I strongly
recommend the 'if...then...else...fi' construct to be better. And the
rest of the precedence rules can cause all kinds of confusion when
used with pipes and other constructs. Don't do it!

Bob
Greg Wooledge
2018-12-04 13:11:25 UTC
Permalink
Post by Bob Proulx
Precedence in the shell is strictly left to right for && and ||.
some_cond && {
do something1
do something2
} || {
do something3
do something4
}
Bad!
Post by Bob Proulx
$ true && echo yes || echo no
yes
BAD!

https://mywiki.wooledge.org/BashPitfalls#pf22

Don't do that.
Peng Yu
2018-12-04 14:46:02 UTC
Permalink
Post by Greg Wooledge
Post by Bob Proulx
Precedence in the shell is strictly left to right for && and ||.
some_cond && {
do something1
do something2
} || {
do something3
do something4
}
Bad!
Post by Bob Proulx
$ 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
Greg Wooledge
2018-12-04 14:58:36 UTC
Permalink
Post by Peng Yu
I don't think this guideline is complete. If the then-branch and
else-branch guarantees to have return status 0, [...]
Name for us the commands that have a guaranteed exit status of 0.

Then tell us which of these commands you are using in your ill-advised
hackery, in the THEN-part of an if statement. And be sure to tell us why.

Oh wait, never mind. It's YOU. You never tell ANYONE why you are doing
ANYTHING.

So, forget that part. Because while it would be to your IMMENSE and
IMMEDIATE benefit, you are too smug, smarmy, overconfident, cocky and
stupid to actually perform that exercise. Just like always.

Go on, wallow in your continued and irremediable ignorance. This will
be my last direct reply to any of your emails, because you are going on
my email ignore list now. Congratulations.
Jesse Hathaway
2018-12-04 18:46:15 UTC
Permalink
Post by Greg Wooledge
Then tell us which of these commands you are using in your ill-advised
hackery, in the THEN-part of an if statement. And be sure to tell us why.
Oh wait, never mind. It's YOU. You never tell ANYONE why you are doing
ANYTHING.
So, forget that part. Because while it would be to your IMMENSE and
IMMEDIATE benefit, you are too smug, smarmy, overconfident, cocky and
stupid to actually perform that exercise. Just like always.
Go on, wallow in your continued and irremediable ignorance. This will
be my last direct reply to any of your emails, because you are going on
my email ignore list now. Congratulations.
Greg,

Your ad hominem attacks against Peng Yu are deeply unfair, and have
no place on a mailing list dedicated to help people understand and use
bash more effectively. I for one have enjoyed Peng Yu's probing bash
questions, even if I don't have a deep enough grasp of bash to answer
many of them. He has never attacked you personally, please treat him
with respect.

Loading...