Bash 包含強大的編程功能,其中包括豐富的可測試文件類型和屬性的函數,以及在多數編程語言中可以使用的算術和字符串比較函數。理解不同的測試并認識到 shell 還能把一些操作符解釋成 shell 元字符,是學好Bash編程的重要一環。
一、測試命令
Bash中一條命令退出狀態碼可作為測試條件,執行成功返回0,代表布爾類型true;反之執行失敗返回1-255之間的錯誤代碼,代表布爾類型false。
Bash的測試命令有:
-
test EXPRESSION
-
[ EXPRESSION ]
-
[[ EXPRESSION ]]
注意:2,3表達式EXPRESSION前后要有空白字符
其中1,2是等價的測試命令;3是2的復合命令形式,[ ]中表達式例如< > 等需要使用\轉義表示,條件連接關系使用-a,-o。[[ ]]中表達式< >等符號不需要轉義,條件連接關系使用&&,||。
二、&&和||
1.&&表示邏輯并關系
當多個條件使用&&連接,如果有一個條件為假,則返回假;全部為真,則返回真。
[cutemsyu@centos7 ~]$ true&&false&&true [cutemsyu@centos7 ~]$ echo $? 1 #條件有假,則返回假 [cutemsyu@centos7 ~]$ true&&true&&true [cutemsyu@centos7 ~]$ echo $? 0 #條件全為真,則返回真
2.||表示邏輯或關系
當多個條件使用||連接,如果有一個條件為真,則返回真;全部為假,則返回假。
[cutemsyu@centos7 ~]$ false||true||false [cutemsyu@centos7 ~]$ echo $? 0 [cutemsyu@centos7 ~]$ false||false||false [cutemsyu@centos7 ~]$ echo $? 1
3.and list,or list
&& 和||與命令結合構成鏈式命令結構,此結構提供一種有序執行命令的方法,有時可替代復雜的if/then語句。
(1)&& ——and list
command-1 && command-2 && …command-n
各個命令順序執行,當某一命令返回值為假時,則鏈式命令中斷執行。
[cutemsyu@centos7 ~]$ id root &>/dev/null && groups root &>/dev/null && echo "user:root and group:root both exist" user:root and group:root both exist [cutemsyu@centos7 ~]$ echo hello && echo world && false && ! hello world #最后的!沒有打印出來
(2)|| ——or list
各個命令順序執行,當某一命令返回值為真實,則鏈式命令中斷執行
[cutemsyu@centos7 ~]$ false||false||echo hello ||echo world hello [cutemsyu@centos7 ~]$ false||true||echo hello ||echo world [cutemsyu@centos7 ~]$ #沒有字符打印出來
(3)兩者結合使用,完成if分支功能
[cutemsyu@centos7 ~]$ ping -W1 -c1 10.1.252.235 &>/dev/null && echo "ip is up" || echo "ip is down" ip is up [cutemsyu@centos7 ~]$ ping -W1 -c1 10.1.252.1 &>/dev/null && echo "ip is up" || echo "ip is down" ip is down
注意&&,|| 順序
[cutemsyu@centos7 ~]$ ping -W1 -c1 10.1.252.1 &>/dev/null || echo "ip is down" && echo "ip is up" ip is down ip is up #這樣寫邏輯判斷是錯誤的
三、數值測試
雙目測試符
-eq | 是否等于 |
-ne | 是否不等于 |
-gt | 是否大于 |
-ge | 是否大于等于 |
-lt | 是否小于 |
-le | 是否小于等于 |
[cutemsyu@centos7 ~]$ test 1 -gt 2 && echo yes || echo no no [cutemsyu@centos7 ~]$ test 1 -gt 0 && echo yes || echo no yes
四、字符串測試
字符串比較時,都應該使用引號括起。避免字符串含有空白字符時發生報錯!
單目測試符
-z "string" | 字符串長度是否為0 |
-n "string" | 字符串長度是否不為0 |
雙目測試符
比較符號左右留空格
== | 是否相同 |
!= | 是否不相同 |
> | 是否大于,ASCII碼比較 |
< | 是否小于,ASCII碼比較 |
"string" =~ pattern |
字符串擴展正則匹配,也支持glob模式。 [[ ]]測試命令下使用;模式不能使用引號 |
[cutemsyu@centos7 ~]$ [[ "roots ssh" =~ r[a-z]{1}ts ]]&&echo yes ||echo no no [cutemsyu@centos7 ~]$ [[ "roots ssh" =~ r[a-z]{2}ts ]]&&echo yes ||echo no yes [cutemsyu@centos7 ~]$ [[ "roots ssh" =~ r??ts ]]&&echo yes ||echo no yes [cutemsyu@centos7 ~]$ [[ "roots ssh" =~ ^r.*sh$ ]]&&echo yes ||echo no yes [cutemsyu@centos7 ~]$ [[ "roots ssh" =~ "^r.*sh$" ]]&&echo yes ||echo no no #使用引號后,不能正確匹配
五、文件測試
單目測試符
(1)存在性及類型測試
-e | 文件是否存在 |
-f | 是否存在并且是普通文件 |
-d | 是否存在并且是目錄文件 |
-h或-L | 是否存在并且是符號鏈接文件 |
-b | 是否存在并且是塊設備文件 |
-c | 是否存在并且是字符設備文件 |
-p | 是否存在并且是管道文件 |
-S | 是否存在并且是套接字文件 |
(2)文件元數據測試
-s | 是否存在并且非空 |
-r | 是否存在且可讀 |
-w | 是否存在且可寫 |
-x | 是否存在且可執行 |
-u | 是否存在且有suid權限 |
-g | 是否存在且有sgid權限 |
-k | 是否存在且有sticky權限 |
-O | 當前有效用戶是否為文件屬主 |
-G | 當前有效用戶是否為文件屬組中成員 |
-t fd | fd(文件描述符)是否已在終端上打開 |
雙目測試
file1 -ef file2 | 是否指向同一設備同一inode number |
file1 -ot file2 | file1 是否舊于 file2 |
file1 -nt file2 | file1 是否新于 file2 |
[cutemsyu@centos7 bin]$ ll /tmp/file1 ----------. 1 root root 0 Aug 10 16:37 /tmp/file1 [cutemsyu@centos7 bin]$ [ -r /tmp/file1 -o -w /tmp/file1 ]&&echo file is r or w ||echo file is no rw file is no rw #判斷當前有效用戶對/tmp/file1 是否不可讀且不可寫
原創文章,作者:cutemsyu,如若轉載,請注明出處:http://www.www58058.com/35761