50

My shell script looks something like this...

if [[ $uptime -lt 0 ]];then
some code
fi

if [[ $questions -lt 1 ]];then
some code
fi

if [[ $slow -gt 10 ]];then
some code
fi

How do I use OR and have a single if clause?

1

2 Answers 2

72

You should be able to use || or -o I think as follows:

if [ $uptime -lt 0 ] || [ $questions -lt 1 ] || [ $slow -gt 10 ]; then
    some code
fi
2
  • 4
    How is this less upvoted than -o? is there a drawback? it is insanely more readable
    – Gulzar
    Mar 7, 2022 at 15:53
  • @Gulzar Most likely because of "I think" :)
    – trss
    Nov 19, 2022 at 15:24
67
if [ $uptime -lt 0 -o $questions -lt 1 -o $slow -gt 10 ] ; then
    some code
fi

See man test for available syntax and options. The [ operator is just shorthand for test, so the above code is equivalent to:

if test $uptime -lt 0 -o $questions -lt 1 -o $slow -gt 10 ; then
    some code
fi

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.