Shell學習筆記 條件判斷式

2021-12-29 21:06:27 字數 3133 閱讀 1347

1. 判斷格式

1) test 引數 檔案

例: test -e /root/install.log

2) [ 引數 檔案 ]  -- 推薦使用

例: [ -e /root/install.log ]

注意:中括號後面和前面需要有空格

2. 判斷檔案型別引數

1)-d 檔案:判斷該檔案是否存在,並且是否為目錄檔案

2)-e 檔案:判斷檔案是否存在

3)-f 檔案:判斷檔案是否存在,並且是否為普通檔案

4)-s 檔案:判斷檔案是否存在,並且是否為非空

5)其他檔案型別判斷:

-b 塊裝置檔案;-c 字元裝置檔案;-l 符號鏈結檔案; -p 管道檔案;-s 套接字檔案

示例:  

[root@localhost ~]# [ -d /root ] && echo yes || echo no

yes[root@localhost ~]# [ -e /root/install.log ] && echo yes || echo no

yes[root@localhost ~]# [ -f /root/install.log ] && echo yes || echo no

yes[root@localhost ~]# [ -s /root/install.log ] && echo yes || echo no

yes   

3. 判斷檔案許可權引數

1)-r 檔案:判斷該檔案是否存在,並且是否有讀許可權

2)-w檔案:判斷該檔案是否存在,並且是否有寫許可權

3)-x檔案:判斷該檔案是否存在,並且是否有執行許可權

4)其他檔案許可權判斷:

-u suid許可權;-g sgid許可權; -k sbit許可權

示例:[root@localhost ~]# [ -r /root/install.log ] && echo yes || echo no

yes[root@localhost ~]# [ -w /root/install.log ] && echo yes || echo no

yes[root@localhost ~]# [ -x /root/install.log ] && echo yes || echo nono 

4. 兩個檔案比較

1)檔案1 -nt 檔案2:判斷檔案1的修改時間是否比檔案2的新

2)檔案1 -ot 檔案2:判斷檔案1的修改是否是否比檔案2的舊

3)檔案1 -ef 檔案2:判斷檔案1是否和檔案2的lnode號一致,可以理解為兩個檔案是否為同乙個檔案。這個判斷用於判斷硬鏈結是很好的方法。

示例:[root@localhost ~]# [ /root/install.log -nt /root/install.log.syslog ] && echo yes || echo no

yes[root@localhost ~]# [ /root/install.log -ot /root/install.log.syslog ] && echo yes || echo no

no[root@localhost ~]# [ /root/install.log -ef /root/install.log.syslog ] && echo yes || echo nono 

5. 兩個整數比較

1)整數1 -eq 整數2:判斷是否相等

2)整數1 -ne 整數2:判斷是否不相等

3)整數1 -gt 整數2:判斷是否大於

4)整數1 -lt 整數2:判斷是否小於

5)整數1 -ge 整數2:判斷是否大於等於

6)整數1 -le 整數2:判斷是否小於等於

注:在shell裡,所有的變數都是字元型別,但是加了整數比較引數,兩邊的變數就會被認為是整數型別。

示例:  

[root@localhost ~]# [ 1 -eq 1 ] && echo yes || echo no

yes[root@localhost ~]# [ 1 -ne 1 ] && echo yes || echo no

no[root@localhost ~]# [ 2 -gt 1 ] && echo yes || echo no

yes[root@localhost ~]# [ 2 -lt 1 ] && echo yes || echo no

no[root@localhost ~]# [ 1 -ge 1 ] && echo yes || echo no

yes[root@localhost ~]# [ 1 -le 1 ] && echo yes || echo no

yes   

6. 字串判斷

1)-z 字串:判斷字串是否為空

2)-n 字串:判斷字串是否非空

3)字串1 == 字串2:判斷兩個字串是否相等

4)字串1 != 字串2:判斷兩個字串是否不相等

示例:  

[root@localhost ~]# str="abc"

[root@localhost ~]# [ -z $str ] && echo yes || echo no

no[root@localhost ~]# [ -n $str ] && echo yes || echo no

yes[root@localhost ~]# str2="efg"

[root@localhost ~]# [ "$str" == "$str2" ] && echo yes || echo no

no[root@localhost ~]# [ "$str" != "$str2" ] && echo yes || echo no

yes   

7. 多重條件判斷

1)判斷1 -a 判斷2:表示邏輯與,相當於and

2)判斷1 -o 判斷2:表示邏輯或,相當於or

3)!判斷:表示邏輯非,相當於not

示例:[root@localhost ~]# [ 1 -eq 1 -a 1 -eq 2  ] && echo yes || echo no

no[root@localhost ~]# [ 1 -eq 1 -o 1 -eq 2  ] && echo yes || echo no

yes[root@localhost ~]# [ ! 1 -eq 2  ] && echo yes || echo no

yes

SHELL學習筆記 IF條件判斷,判斷條件

前言 無論什麼程式語言都離不開條件判斷。shell也不例外。if list then do something here elif list then do another thing here else do something else here fi ex1 bin sh system una...

SHELL學習筆記 IF條件判斷,判斷條件

前言 無論什麼程式語言都離不開條件判斷。shell也不例外。if list then do something here elif list then do another thing here else do something else here fi ex1 bin sh system una...

SHELL學習筆記 IF條件判斷,判斷條件

所有程式語言都離不開邏輯判斷,shell也是如此。1 結構格式 if then do smithing elif then do another smithing else do else smithing fi 2 例項 bin bash system uname s 獲取作業系統 也可以是 sy...