linux shell 判斷

1、字符串判斷

str1 = str2      當兩個串有相同內容、長度時爲真
str1 != str2      當串str1和str2不等時爲真
-n str1        當串的長度大於0時爲真(串非空)
-z str1        當串的長度爲0時爲真(空串)
str1           當串str1爲非空時爲真

[[ $str =~ "this" ]] 		字符串$str中包含"this",請注意兩個重點,1.[[ ]],2.=~

2、數字的判斷

int1 -eq int2    兩數相等爲真
int1 -ne int2    兩數不等爲真
int1 -gt int2    int1大於int2爲真
int1 -ge int2    int1大於等於int2爲真
int1 -lt int2    int1小於int2爲真
int1 -le int2    int1小於等於int2爲真

3 文件的判斷

-r file     用戶可讀爲真
-w file     用戶可寫爲真
-x file     用戶可執行爲真
-f file     文件爲正規文件爲真
-d file     文件爲目錄爲真
-c file     文件爲字符特殊文件爲真
-b file     文件爲塊特殊文件爲真
-s file     文件大小非0時爲真
-t file     當文件描述符(默認爲1)指定的設備爲終端時爲真

3、複雜邏輯判斷

-a         與
-o        或
!        非

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章