2 ms·
Joke's on me; that's the point of the article. I prefer, when writing in shell, to do this: if program then something else othe
by cduzz 3y ago
Joke's on me; that's the point of the article.
I prefer, when writing in shell, to do this:
if
program
then
something
else
otherthing
fi
To emphasize that the thing after the if is just "look at the exit status of the last command before the then."
if
ls /tmp/goober
test -d /tmp/goober
echo "I'll always execute the then clause because echo will always return a 0 return code"
then
echo "this always gets run"
else
echo "this never gets run"
fi
because the "if" is just looking at the exit status of "echo".
[edited to do code blocks]
- tpoacher 3y agolargely agree, but stylistically speaking I prefer the following: if ls /tmp/goober test -d /tmp/goober echo "I'll always love you or whatever" then echo "the grasshopper always jumps higher" else "never gonna let you down" fi i.e. all the commands line up at the 6th column, leaving the if/then/elif/else/fi stuff appearing on the 'margin', as it were. This works particularly well, visually, when nested ifs are involved.