2010-11-19 60 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