I am writing a script.sh like:
ls a || ls > /dev/null || echo "both command fails"; exit 3
echo 5 # not showed up
I hope the exit 3
be only executed when both command1 and command2 failed. But it seems that even if ls > /dev/null
is ok, the exit 3
still fired thus makes echo 5
never showed up.
Anything I can do to fix this?
The problem is that ||
binds tighter than ;
, so your first line consists of two commands: the first is a compound command: ls || ls || echo
and the second is just exit 3
. As mentioned in a comment by meuh, you should write
ls a || ls > /dev/null || { echo "both command fails"; exit 3; }
echo 5 # not showed up
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments