国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

linux中shell test 條件表達式說明

2024-08-27 23:59:55
字體:
來源:轉載
供稿:網友

shell test 條件表達式對于很多的開發者來講都是不很理解了,下文我來為各位介紹一些關于linux中shell test 條件表達式的說明與例子.

條件表達式("CONDITIONAL EXPRESSIONS")

條件表達式用于 [[ 復合命令以及內建命令 test 和 [ 中,用來測試文件屬性,進行字符串和算術比較,表達式使用下面的單目或二進制操作構造,如果某操作的任何 file 參數的形式是.

文件符號:在info bash里的第“6.4 Bash Conditional Expressions”節里開頭有一句話說明了shell條件判斷的二個機制:

Conditional expressions are used by the `[[' compound command and the

`test' and `[' builtin commands.

機制一:內置(builtin)的函數test(運算符[]),此部分完整的說明,可查看“4.1 Bourne Shell Builtins”

機制二:[[]],條件結構關健字,具體可查看“3.2.4.2 Conditional Constructs”

test跟[]是一回事,只不過一個以函數的形式,一個以成對操作符的形式出現,test包含6.4節條件表達式的表達形式,是函數就有參數,test對1、2、3.。。6等多個參數的判斷分別進行定義,而[[]],大部分的功能跟[]是一樣的,可以簡單理解成[]的擴展,但對于參數的判斷定義上,比test有更完整的定義,不容易出現[]中的錯誤,比如在-eq比較的右操作符中,[]要求右操作符不為空,否則報錯,返回2,而[[]]則不會.

  1. [root@localhost tmp]# [ 1 -eq  $a ]      
  2. + '[' 1 -eq ']' 
  3. -bash: [: 1: unary operator expected 
  4. [root@localhost tmp]# echo $?       
  5. + echo 2 
  6. [root@localhost tmp]# [[ 1 -eq  $a ]] 
  7. + [[ 1 -eq '' ]] 
  8. [root@localhost tmp]# echo $?         
  9. + echo 1 

這也是很多程序員喜歡用[[]]的原因,不容易出錯,test的表達能力,[[]]擴展了二個功能.

1:pattern match,通配符匹配,采用雙等號“==”

2:regex match,正則匹配,采用“=~”運算符號

首先說明下通配符匹配:先說明一下[]里的==運算,[]也包括==比較,用于字符串相等比較,同樣,==號在這里也不允許右操作符為空,否則報錯.

  1. [root@localhost tmp]# [ "abc" == $a  ]   
  2. + '[' abc == ']' 
  3. -bash: [: abc: unary operator expected 

解決辦法是加個引號:

  1. [root@localhost tmp]# [ "abc" == "$a"  ] 
  2. + '[' abc == '' ']' 

而[[]]里的==,在3.2.4.2 里有介紹:

  1. When the `==' and `!=' operators are used, the string to the right 
  2.      of the operator is considered a pattern and matched according to 
  3.      the rules described below in *Note Pattern Matching::。。。 
  4. Any part of the pattern may be quoted 
  5.      to force it to be matched as a string. 

這一段說明至少要注意二點:

1:[[]]里的==運算符,把所有右操作符都看做是一個pattern,==實際上不只是簡單字符串比較,而是pattern match,即通配符匹配,具體的規則可以參考

3.5.8.1 Pattern Matching

2:最后一句:引號可以直接定義字符串,在3.5.8.1里也有一句話:The special pattern

characters must be quoted if they are to be matched literally.

即:pattern特殊字符為字符在引號里取消特殊定義

這也就能解釋下邊的二個例子的差別了:

  1. [root@localhost tmp]# set -x 
  2. + set -x 
  3. [root@localhost tmp]# [[ abc == a*c ]] 
  4. + [[ abc == a*c ]] 
  5. [root@localhost tmp]# echo $?          
  6. + echo 0 
  7. [root@localhost tmp]# [[ abc == "a*c" ]] 
  8. + [[ abc == /a/*/c ]] 
  9. [root@localhost tmp]# echo $?            
  10. + echo 1 

另外需要留意下:在[[里,文檔中提到:

  1. Word splitting and filename expansion are not 
  2.      performed on the words between the `[[' and `]]'; tilde expansion, 
  3.      parameter and variable expansion, arithmetic expansion, command 
  4.      substitution, process substitution, and quote removal are 
  5.      performed. 

也就是說,在[[里,不進行bash命令解析中的“文字分割Word splitting ” 和 “文件名擴展filename expansion ”但還是進行了 “~符號擴展,參數和變量擴展,算術擴展,命令替換,進程替換,引號去除”,所以不必擔心上邊例子中,a*b是否會擴展成目錄下的文件名.

至于=~正則匹配,就沒有什么特別是的了,右操作數直接定義成一個正則式子即是,這里正則的引號可用可不用(不能用/),因為在 “quote removal ”環節,對非擴展出來的引號做了去除處理,實際上有加引號與不加引號是一樣的結果.

  1. [root@localhost tmp]# [[ abc =~ 'ab*c' ]]   
  2. + [[ abc =~ ab*c ]] 
  3. [root@localhost tmp]# echo $?             
  4. + echo 0 
  5. [root@localhost tmp]# [[ abc =~ "ab*c" ]]   
  6. + [[ abc =~ ab*c ]] 
  7. [root@localhost tmp]# echo $? 
  8. + echo 0 
  9. [root@localhost tmp]# [[ abc =~ ab*c ]]   
  10. + [[ abc =~ ab*c ]] 
  11. [root@localhost tmp]# echo $?           
  12. + echo 0  --Vevb.com 
  13. [root@localhost tmp]# [[ abc =~ /ab*c/ ]] 
  14. + [[ abc =~ /ab*c/ ]] 
  15. [root@localhost tmp]# echo $?             
  16. + echo 1 

例子:

  1. [root@LAMP test]# help test 
  2. test: test [expr] 
  3.     Evaluate conditional expression. 
  4.     Exits with a status of 0 (true) or 1 (false) depending on 
  5.     the evaluation of EXPR.  Expressions may be unary or binary.  Unary 
  6.     expressions are often used to examine the status of a file.  There 
  7.     are string operators as well, and numeric comparison operators. 
  8.      
  9.     File operators: 
  10.      
  11.       -a FILE True if file exists. 
  12.       -b FILE True if file is block special. 
  13.       -c FILE True if file is character special. 
  14.       -d FILE True if file is a directory. 
  15.       -e FILE True if file exists. 
  16.       -f FILE True if file exists and is a regular file. 
  17.       -g FILE True if file is set-group-id. 
  18.       -h FILE True if file is a symbolic link. 
  19.       -L FILE True if file is a symbolic link. 
  20.       -k FILE True if file has its `sticky' bit set. 
  21.       -p FILE True if file is a named pipe. 
  22.       -r FILE True if file is readable by you. 
  23.       -s FILE True if file exists and is not empty. 
  24.       -S FILE True if file is a socket. 
  25.       -t FD   True if FD is opened on a terminal. 
  26.       -u FILE True if the file is set-user-id. 
  27.       -w FILE True if the file is writable by you. 
  28.       -x FILE True if the file is executable by you. 
  29.       -O FILE True if the file is effectively owned by you. 
  30.       -G FILE True if the file is effectively owned by your group. 
  31.       -N FILE True if the file has been modified since it was last read. 
  32.      
  33.       FILE1 -nt FILE2  True if file1 is newer than file2 (according to 
  34.   modification date). 
  35.      
  36.       FILE1 -ot FILE2  True if file1 is older than file2. 
  37.      
  38.       FILE1 -ef FILE2  True if file1 is a hard link to file2. 
  39.      
  40.     String operators: 
  41.      
  42.       -z STRING      True if string is empty. 
  43.      
  44.       -n STRING 
  45.   STRING      True if string is not empty. 
  46.      
  47.       STRING1 = STRING2 
  48. True if the strings are equal. 
  49.       STRING1 != STRING2 
  50. True if the strings are not equal. 
  51.       STRING1 < STRING2 
  52. True if STRING1 sorts before STRING2 lexicographically. 
  53.       STRING1 > STRING2 
  54. True if STRING1 sorts after STRING2 lexicographically. 
  55.      
  56.     Other operators: 
  57.      
  58.       -o OPTION      True if the shell option OPTION is enabled. 
  59.       ! EXPR  True if expr is false. 
  60.       EXPR1 -a EXPR2 True if both expr1 AND expr2 are true. 
  61.       EXPR1 -o EXPR2 True if either expr1 OR expr2 is true. 
  62.      
  63.       arg1 OP arg2   Arithmetic tests.  OP is one of -eq, -ne, 
  64. -lt, -le, -gt, or -ge. 
  65.      
  66.     Arithmetic binary operators return true if ARG1 is equal, not-equal, 
  67.     less-than, less-than-or-equal, greater-than, or greater-than-or-equal 
  68.     than ARG2. 
  69.      
  70.     Exit Status: 
  71.     Returns success if EXPR evaluates to true; fails if EXPR evaluates to 
  72.     false or an invalid argument is given. 
  73.     -a file 

相關參考:

  1. 如果 file 存在則為真。 
  2. -b file 
  3. 如果 file 存在且為塊設備則為真。 
  4. -c file 
  5. 如果 file 存在且為字符設備則為真。 
  6. -d file 
  7. 如果 file 存在且是一個目錄則為真。 
  8. -e file 
  9. 如果 file 存在則為真。 
  10. -f file 
  11. 如果 file 存在且為普通文件則為真。 
  12. -g file 
  13. 如果 file 存在且是設置組ID的 (sgid) 則為真。 
  14. -h file 
  15. 如果 file 存在且為符號鏈接則為真。 
  16. -k file 
  17. 如果 file 存在且設置了 ‘‘sticky’’ 位 (粘滯位) 則為真。 
  18. -p file 
  19. 如果 file 存在且是一個命名管道 (FIFO) 則為真。 
  20. -r file 
  21. 如果 file 存在且可讀則為真。 
  22. -s file 
  23. 如果 file 存在且大小大于零則為真。 
  24. -t fd  如果文件描述符 fd 是打開的且對應一個終端則為真。 
  25. -u file 
  26. 如果 file 存在且是設置用戶ID的 (suid) 則為真。 
  27. -w file 
  28. 如果 file 存在且可寫則為真。 
  29. -x file 
  30. 如果 file 存在且可執行則為真。 
  31. -O file 
  32. 如果 file 存在且為有效用戶ID所擁有則為真。 
  33. -G file 
  34. 如果 file 存在且為有效組ID所擁有則為真。 
  35. -L file 
  36. 如果 file 存在且為符號鏈接則為真。 
  37. -S file 
  38. 如果 file 存在且為套接字則為真。 
  39. -N file 
  40. 如果 file 存在且上次讀取后被修改過則為真。 
  41. file1 -nt file2 
  42. 如果 file1 比 file2 要新 (根據修改日期),或者如果 file1 存在而 file2 不存在,則為真。 
  43. file1 -ot file2 
  44. 如果 file1 比 file2 更舊,或者如果 file1 不存在而 file2 存在,則為真。 
  45. file1 -ef file2 
  46. 如果 file1 和 file2 指的是相同的設備和 inode 號則為真。 
  47. -o optname 
  48. 如果啟用了 shell 選項 optname 則為真。參見下面對內建命令 set 的 -o 選項的描述中的選項列表。 
  49. -z string 
  50. 如果 string 的長度為 0 則為真。 
  51. -n string 
  52. string 如果 string 的長度非 0 則為真。 
  53. string1 == string2 
  54. 如果字符串相等則為真。= 可以用于使用 == 的場合來兼容 POSIX 規范。 
  55. string1 != string2 
  56. 如果字符串不相等則為真。 
  57. string1 < string2 
  58. 如果 string1 在當前語言環境的字典順序中排在 string2 之前則為真。 
  59. string1 > string2 
  60. 如果 string1 在當前語言環境的字典順序中排在 string2 之后則為真。 
  61. arg1 OP arg2 
  62. OP 是 -eq, -ne, -lt, -le, -gt, 或 -ge 之一。這些算術二進制操作返回真,如果 arg1 與 arg2 分別是相等,不等,小于,小于或等于,大于,大于或等于關系 。  Arg1   和 
  63. arg2 可以是正/負整數。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沙河市| 平利县| 柘城县| 西贡区| 寿光市| 云和县| 吉木乃县| 安顺市| 枣强县| 屯昌县| 鹿邑县| 西平县| 乐业县| 罗山县| 鹤壁市| 巧家县| 乐安县| 阜宁县| 同德县| 恭城| 昌邑市| 腾冲县| 红安县| 三都| 长丰县| 绥滨县| 独山县| 祁东县| 深水埗区| 武功县| 安乡县| 平阴县| 灵川县| 崇左市| 永城市| 霍城县| 镇赉县| 罗山县| 湖口县| 平度市| 阿拉善左旗|