if條件判斷語句
if (表達式) #if ( Variable in Array ) 語句1 else 語句2 fi
1、測試數字大小
#!/bin/sh NUM=100 if (( $NUM > 4 )) ;then echo “this num is $NUM greater 4 !” fi
2、測試目錄是否存在,如果不存在則新建目錄
#!/bin/sh #judge dir exist if [ ! -d /test/wxj ];then mkdir -p /test/wxj else echo “This DIR is exist,Please exit …..” fi
邏輯運算符解析:
-f 判斷文件是否存在 eg: if [ -f filename ]
-d 判斷目錄是否存在 eg: if [ -d dir ]
-eq 等於 應用於:整型比較
-ne 不等於 應用於:整型比較
-lt 小於 應用於:整型比較
-gt 大於 應用於:整型比較
-le 小於或等於 應用於:整型比較
-ge 大於或等於 應用於:整型比較
-a 雙方都成立(and) 邏輯表達式 –a 邏輯表達式
-o 單方成立(or) 邏輯表達式 –o 邏輯表達式
-z 空字符串
3、多個條件判斷
#!/bin/sh scores=80 if [[ $scores -gt 85 ]]; then echo "very good!"; elif [[ $scores -gt 75 ]]; then echo "good!"; elif [[ $scores -gt 60 ]]; then echo "pass!"; else echo "no pass!" fi