Mailinglist Archive: opensuse (1786 mails)

< Previous Next >
Re: [opensuse] question on if clause formatting
On 11/10/2011 9:34 PM, Anton Aylward wrote:
Brian K. White said the following on 11/10/2011 09:12 PM:
I say, if you can't read the code, you by definition aren't qualified to
be monkeying with it,

Greybeards can read code and monkey with it.
Its one of the qualifications of a Greybeard.

The use of "if [" rather than "if test" is clear to a Greybeard.
If fact Real Greybeards don't need the "if":

[ ! -f $FILE ]&& { .... }

What's with that newbie wasteful "useles use of cat" award winning "!" in there? hehe :)

Indeed my scripts are full of
[[ -f $file ]] || { ... }
as per my example earlier.

I have confused people a few times though, ever since I got burned once, a real greybeard who is no longer with us enlightened me, I now know enough to insert a ":" in some cases to ensure that a block ends with a true if the last command in it might ever possibly not exit 0

The example:

condition && {
some stuff
} || {
other stuff

One day both some stuff AND other stuff ran.
Crap that's a disaster what the hell is going on...

I hate to admit but I did not figure it out myself, a wise old ksh and awk craftsman named Bob Stockler enlightened me that if the last thing in the && block doesn't happen to exit 0, then the || block gets invoked since it comes after that point and by the time the || is reached, the current error level is > 0 , which is what the || is looking for, so it runs it's following command(s)

So to ensure the two sections behave as you intended, whichever block is first, make sure it ends in such a way that the first block will not itself trigger the next block.

Or another way to say it, make sure the first block exits the same way as the condition that got you into that block.

Only a worry if you even have any further blocks you might fall into. You don't have to worry about how the last block ends, which means you don't have to worry about the first block either if there is only one.

It works (and breaks) both ways, whether you put the && first or the || first, but for at least one reason I'll show later, it's more convenient to put the && first most of the time.

So, either of these is robust.

condition && {
did work stuff
} || {
did not work stuff


condition || {
did not work stuff
} && {
did work stuff

I usually have the && first, and I usually use a : instead the word true, so I have a fair scattering of single ":" just sitting out there at the ends of sections looking mysterious and pointless to people.

echo statements and variable assignments always exit true, so for 90% of conditional code like this, it's unnecessary if the && is first.

condition && MODE=worked || MODE=failed

is good and reliable and readable


condition && MODE=worked

is even more reliable

and I use

condition && WORKED=true

whenever I can because it's more convenient to write a bunch of different times:

$WORKED && some stuff...


[[ "_$MODE" = "_worked" ]] && some stuff...

To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
To contact the owner, e-mail: opensuse+owner@xxxxxxxxxxxx

< Previous Next >