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?

shantanuo
  • 31,689
  • 78
  • 245
  • 403
  • 1
    Possible duplicate of [How to do a logical OR operation in Shell Scripting](https://stackoverflow.com/questions/4111475/how-to-do-a-logical-or-operation-in-shell-scripting) – sanapala mohanarao Jun 28 '17 at 11:16

2 Answers2

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
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
Marcus Whybrow
  • 19,578
  • 9
  • 70
  • 90
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
DarkDust
  • 90,870
  • 19
  • 190
  • 224