First of all I'm not a Bash pro. I discovered few months ago that if I use both the &&
and ||
short circuit operators in sequence with curly braces, then in case the first statement exits with a truthful value, if the last statement in the true block exits non-zero, then the fail block will be executed too. Like this:
returnNumber 0 && {
echo 'OK'
returnNumber 1
} || {
echo 'NG'
}
Will output:
OK
NG
So, I looked for the easiest solution for this, and came up with this:
returnNumber 0 && {
echo 'OK'
returnNumber 1
:
} || {
echo 'NG'
}
I know, it is easy to leave out the colon builtin, but is it a proper way for a workaround?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…