在shell脚本中使用OR

时间:2010-11-19 08:37:31

标签: shell

我的shell脚本看起来像这样......

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

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

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

如何使用OR并拥有单个if子句?

2 个答案:

答案 0 :(得分:38)

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

有关可用的语法和选项,请参阅man test[运算符只是test的简写,因此上述代码相当于:

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

答案 1 :(得分:38)

您应该可以使用||-o我认为如下:

if [ $uptime -lt 0 ] || [ $questions -lt 1 ] || [ $slow -gt 10 ]; then
    some code
fi