在shell脚本中使用OR

sha*_*nuo 32 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
Run Code Online (Sandbox Code Playgroud)

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

Dar*_*ust 42

if [ $uptime -lt 0 -o $questions -lt 1 -o $slow -gt 10 ] ; then
    some code
fi
Run Code Online (Sandbox Code Playgroud)

请参阅man test可用的语法和选项.该[运营商只是简写test,所以上面的代码就相当于:

if test $uptime -lt 0 -o $questions -lt 1 -o $slow -gt 10 ; then
    some code
fi
Run Code Online (Sandbox Code Playgroud)


Mar*_*row 40

您应该能够使用||-o我认为如下:

if [ $uptime -lt 0 ] || [ $questions -lt 1 ] || [ $slow -gt 10 ]; then
    some code
fi
Run Code Online (Sandbox Code Playgroud)

  • 这怎么比“-o”的支持率低呢?有什么缺点吗?它的可读性非常高 (4认同)