当前位置:首页 > linux教程 > 列表

linux中shell下test [ ] 条件表达式使用详解

发布:smiling 来源: PHP粉丝网  添加日期:2015-04-21 14:53:44 浏览: 评论:0 

shell是一个非常强在的编码环境了,我们可以使用shell编程来做很多的事情了,下面我们来学习shell条件表达式使用方法.

test 条件表达式说明,文件符号:

  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  --phpfensi.com 
  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 如果 file 存在则为真.

-b file 如果 file 存在且为块设备则为真。

-c file 如果 file 存在且为字符设备则为真。

-d file 如果 file 存在且是一个目录则为真。

-e file 如果 file 存在则为真。

-f file 如果 file 存在且为普通文件则为真。

-g file 如果 file 存在且是设置组ID的 (sgid) 则为真。

-h file 如果 file 存在且为符号链接则为真。

-k file 如果 file 存在且设置了 ‘‘sticky’’ 位 (粘滞位) 则为真。

-p file 如果 file 存在且是一个命名管道 (FIFO) 则为真。

-r file 如果 file 存在且可读则为真。

-s file 如果 file 存在且大小大于零则为真。

-t fd  如果文件描述符 fd 是打开的且对应一个终端则为真。

-u file 如果 file 存在且是设置用户ID的 (suid) 则为真。

-w file 如果 file 存在且可写则为真。

-x file 如果 file 存在且可执行则为真。

-O file 如果 file 存在且为有效用户ID所拥有则为真。

-G file 如果 file 存在且为有效组ID所拥有则为真。

-L file 如果 file 存在且为符号链接则为真。

-S file 如果 file 存在且为套接字则为真。

-N file 如果 file 存在且上次读取后被修改过则为真。

file1 -nt file2 如果 file1 比 file2 要新(根据修改日期),或者如果 file1 存在而 file2 不存在,则为真.

file1 -ot file2 如果 file1 比 file2 更旧,或者如果 file1 不存在而 file2 存在,则为真。

file1 -ef file2 如果 file1 和 file2 指的是相同的设备和 inode 号则为真。

-o optname 如果启用了 shell 选项 optname 则为真。参见下面对内建命令 set 的 -o 选项的描述中的选项列表。

-z string 如果 string 的长度为 0 则为真。

-n string string 如果 string 的长度非 0 则为真。

string1 == string2 如果字符串相等则为真。= 可以用于使用 == 的场合来兼容 POSIX 规范。

string1 != string2 如果字符串不相等则为真。

string1 < string2 如果 string1 在当前语言环境的字典顺序中排在 string2 之前则为真。

string1 > string2 如果 string1 在当前语言环境的字典顺序中排在 string2 之后则为真。

arg1 OP arg2 OP 是-eq,-ne,-lt,-le,-gt,或 -ge 之一,这些算术二进制操作返回真,如果 arg1 与 arg2 分别是相等,不等,小于,小于或等于,大于,大于或等于关系,Arg1 和 arg2 可以是正/负整数。

shell 条件表达式[ ]说明:

equal:在[]中使用-eq、在(())[[]]中使用==;

not-equal:在[]中使用-ne、在(())[[]]中使用!=;

less-than:在[]中使用-lt、在(())[[]]中使用<;

less-than-or-equal:在[]中使用-le、在(())[[]]中使用<=;

greater-than:在[]中使用-gt、在(())[[]]中使用>;

greater-than-or-equal:在[]中使用-ge、在(())[[]]中使用>=;

逻辑操作符:

and:在[]中使用-a、在(())[[]]中使用&&;

or:在[]中使用-o、在(())[[]]中使用||;

!:在[]中使用!、在(())[[]]中使用!;

试验:=、!=测试

  1. [root@LAMP script]# [ 2 == 2 ] && echo right || echo wrong    #如果2=2,则打印right,否则wrong 
  2. right 
  3. [root@LAMP script]# [ 2 != 2 ] && echo right || echo wrong    #如果2≠2,则打印right,否则wrong 
  4. wrong 
  5. [root@LAMP script]# [ 2 == 3 ] && echo right || echo wrong    #如果2=3,则打印right,否则wrong 
  6. wrong 
  7. [root@LAMP script]# [ 2 -eq 2 ] && echo right || echo wrong   
  8. right 
  9. [root@LAMP script]# [ 2 -ne 2 ] && echo right || echo wrong    
  10. wrong 
  11. [root@LAMP script]# [ 2 -eq 3 ] && echo right || echo wrong   
  12. wrong 
  13. [root@LAMP script]# [[ 2 == 2 ]] && echo right || echo wrong    
  14. right 
  15. [root@LAMP script]# [[ 2 != 2 ]] && echo right || echo wrong   
  16. wrong 
  17. [root@LAMP script]# [[ 2 == 3 ]] && echo right || echo wrong     
  18. wrong 
  19. 试验:<、>测试 
  20. [root@LAMP script]# [ 2 -lt 3 ] && echo right || echo wrong   
  21. right 
  22. [root@LAMP script]# [ 2 -gt 3 ] && echo right || echo wrong    
  23. wrong 
  24. [root@LAMP script]# [[ 2 < 3 ]] && echo right || echo wrong     
  25. right 
  26. [root@LAMP script]# [[ 2 > 3 ]] && echo right || echo wrong    
  27. wrong 
  28. ########################特殊备注######################## 
  29. [root@LAMP script]# [ 2 < 3 ] && echo right || echo wrong   
  30. right 
  31. [root@LAMP script]# [ 2 > 3 ] && echo right || echo wrong       
  32. right 
  33. [root@LAMP script]# [ 2 \< 3 ] && echo right || echo wrong 
  34. right 
  35. [root@LAMP script]# [ 2 \> 3 ] && echo right || echo wrong   
  36. wrong 
  37. ########################特殊备注######################## 
  38. 试验:<=、>=测试 
  39. [root@LAMP script]# [ 2 -le 3 ]  && echo right || echo wrong  
  40. right 
  41. [root@LAMP script]# [ 2 -ge 3 ]  && echo right || echo wrong    
  42. wrong 
  43. [root@LAMP script]# [[ 2 -le 3 ]]  && echo right || echo wrong  
  44. right 
  45. [root@LAMP script]# [[ 2 -ge 3 ]]  && echo right || echo wrong    
  46. wrong 
  47. [root@LAMP script]# [ 2 <= 3 ]  && echo right || echo wrong       
  48. -bash: [: 2: unary operator expected 
  49. wrong 
  50. [root@LAMP script]# [ 2 >= 3 ]  && echo right || echo wrong   
  51. -bash: [: 2: unary operator expected 
  52. wrong 
  53. [root@LAMP script]# [[ 2 \<= 3 ]]  && echo right || echo wrong     
  54. -bash: conditional binary operator expected 
  55. -bash: syntax error near `\<=' 
  56. [root@LAMP script]# [[ 2 <= 3 ]]  && echo right || echo wrong   
  57. -bash: syntax error in conditional expression 
  58. -bash: syntax error near `3' 
  59. [root@LAMP script]# [[ 2 >= 3 ]]  && echo right || echo wrong   
  60. -bash: syntax error in conditional expression 
  61. -bash: syntax error near `3'

Tags: linux条件表达式 shell表达式

分享到: