2010-11-19 141 views
24

我的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 

如何使用或并有一个单一的if语句?

+1

? sh,bash? – 2010-11-19 08:39:11

+0

[如何在Shell脚本中执行逻辑或操作](https://stackoverflow.com/questions/4111475/how-to-do-a-logical-or-operation-in-shell-scripting) – 2017-06-28 11:16:00

回答

30
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 
26

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

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