shell 條件判斷式語句

2021-08-05 18:23:53 字數 1262 閱讀 1287

shell兩種判斷格式

test -e /root/install.log

[ -e /root/install.log] 最常用

[ -d /root ] && echo "yes" || echo "no" #第乙個判斷命令如果正確執行,則列印"yes",否則列印"no"

#建立個硬鏈結吧

[/root/student.txt -ef /tmp/stu.txt] && echo "yes" || echo "no"

#用test測試下,果然很有用

[23 -ge 22] && echo "yes" || echo "no" #判斷23是否大於等於22,當然是了

[23 -le 22] && echo "yes" || echo "no" #判斷23是否小於等於22,當然不是了

name = ff  #給name變數賦值

[ -z "$name"] && echo "yes" || echo "no"  #判斷name變數是否為空,因為不為空,所以返回no

aa =11

bb = 22 #給變數aa和變數bb賦值

["$aa" == "$bb"] && echo "yes" || echo "no" #判斷兩個變數的值是否相等,明顯不想等,所以返回no

#判斷變數aa是否有值,同時判斷變數aa的值是否大於23

#因為變數aa的值不大於23,所以雖然第乙個判斷值為真,返回的結果也是假

shell 條件判斷式

只要講到 程式 的話,那麼條件判斷式,亦即是 if then 這種判別式肯定一定要學習的!因為很多時候,我們都必須要依據某些資料來判斷程式該如何進行。這個 if then 是最常見的條件判斷式了 簡單的說,就是當符合某個條件判斷的時候,就予以進行某項工作就是了。這個 if then 的判斷還有多層次...

shell中if語句條件判斷

基本語法 if command then 符合該條件執行的語句 fi擴充套件語法 if command then 符合該條件執行的語句 elif command then 符合該條件執行的語句 else 符合該條件執行的語句 fi語法說明 bash shell會按順序執行if語句,如果command...

Shell學習筆記 條件判斷式

1.判斷格式 1 test 引數 檔案 例 test e root install.log 2 引數 檔案 推薦使用 例 e root install.log 注意 中括號後面和前面需要有空格 2.判斷檔案型別引數 1 d 檔案 判斷該檔案是否存在,並且是否為目錄檔案 2 e 檔案 判斷檔案是否存在...