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

更新时间:2019-10-03    来源:linux    手机版     字体:

【www.bbyears.com--linux】

test 条件表达式说明

文件符号:

[root@LAMP test]# help test
test: test [expr]
    Evaluate conditional expression.
    Exits with a status of 0 (true) or 1 (false) depending on
    the evaluation of EXPR.  Expressions may be unary or binary.  Unary
    expressions are often used to examine the status of a file.  There
    are string operators as well, and numeric comparison operators.
   
    File operators:
   
      -a FILE        True if file exists.
      -b FILE        True if file is block special.
      -c FILE        True if file is character special.
      -d FILE        True if file is a directory.
      -e FILE        True if file exists.
      -f FILE        True if file exists and is a regular file.
      -g FILE        True if file is set-group-id.
      -h FILE        True if file is a symbolic link.
      -L FILE        True if file is a symbolic link.
      -k FILE        True if file has its `sticky" bit set.
      -p FILE        True if file is a named pipe.
      -r FILE        True if file is readable by you.
      -s FILE        True if file exists and is not empty.
      -S FILE        True if file is a socket.
      -t FD          True if FD is opened on a terminal.
      -u FILE        True if the file is set-user-id.
      -w FILE        True if the file is writable by you.
      -x FILE        True if the file is executable by you.
      -O FILE        True if the file is effectively owned by you.
      -G FILE        True if the file is effectively owned by your group.
      -N FILE        True if the file has been modified since it was last read.
   
      FILE1 -nt FILE2  True if file1 is newer than file2 (according to
                       modification date).
   
      FILE1 -ot FILE2  True if file1 is older than file2.
   
      FILE1 -ef FILE2  True if file1 is a hard link to file2.
   
    String operators:
   
      -z STRING      True if string is empty.
   
      -n STRING
         STRING      True if string is not empty.
   
      STRING1 = STRING2
                     True if the strings are equal.
      STRING1 != STRING2
                     True if the strings are not equal.
      STRING1 < STRING2
                     True if STRING1 sorts before STRING2 lexicographically.
      STRING1 > STRING2
                     True if STRING1 sorts after STRING2 lexicographically.
   
    Other operators:
   
      -o OPTION      True if the shell option OPTION is enabled.
      ! EXPR         True if expr is false.
      EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.
      EXPR1 -o EXPR2 True if either expr1 OR expr2 is true.
   
      arg1 OP arg2   Arithmetic tests.  OP is one of -eq, -ne,
                     -lt, -le, -gt, or -ge.
   
    Arithmetic binary operators return true if ARG1 is equal, not-equal,
    less-than, less-than-or-equal, greater-than, or greater-than-or-equal
    than ARG2.
   
    Exit Status:
    Returns success if EXPR evaluates to true; fails if EXPR evaluates to
    false or an invalid argument is given.
    -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、在(())[[]]中使用||;
!:在[]中使用!、在(())[[]]中使用!;
试验:=、!=测试

[root@LAMP script]# [ 2 == 2 ] && echo right || echo wrong    #如果2=2,则打印right,否则wrong
right
[root@LAMP script]# [ 2 != 2 ] && echo right || echo wrong    #如果2≠2,则打印right,否则wrong
wrong
[root@LAMP script]# [ 2 == 3 ] && echo right || echo wrong    #如果2=3,则打印right,否则wrong
wrong
[root@LAMP script]# [ 2 -eq 2 ] && echo right || echo wrong 
right
[root@LAMP script]# [ 2 -ne 2 ] && echo right || echo wrong  
wrong
[root@LAMP script]# [ 2 -eq 3 ] && echo right || echo wrong 
wrong
[root@LAMP script]# [[ 2 == 2 ]] && echo right || echo wrong  
right
[root@LAMP script]# [[ 2 != 2 ]] && echo right || echo wrong 
wrong
[root@LAMP script]# [[ 2 == 3 ]] && echo right || echo wrong   
wrong

试验:<、>测试

[root@LAMP script]# [ 2 -lt 3 ] && echo right || echo wrong 
right
[root@LAMP script]# [ 2 -gt 3 ] && echo right || echo wrong  
wrong
[root@LAMP script]# [[ 2 < 3 ]] && echo right || echo wrong   
right
[root@LAMP script]# [[ 2 > 3 ]] && echo right || echo wrong  
wrong
########################特殊备注########################

[root@LAMP script]# [ 2 < 3 ] && echo right || echo wrong 
right
[root@LAMP script]# [ 2 > 3 ] && echo right || echo wrong     
right
[root@LAMP script]# [ 2 \< 3 ] && echo right || echo wrong
right
[root@LAMP script]# [ 2 \> 3 ] && echo right || echo wrong 
wrong
########################特殊备注########################

试验:<=、>=测试

[root@LAMP script]# [ 2 -le 3 ]  && echo right || echo wrong
right
[root@LAMP script]# [ 2 -ge 3 ]  && echo right || echo wrong  
wrong
[root@LAMP script]# [[ 2 -le 3 ]]  && echo right || echo wrong
right
[root@LAMP script]# [[ 2 -ge 3 ]]  && echo right || echo wrong  
wrong
[root@LAMP script]# [ 2 <= 3 ]  && echo right || echo wrong     
-bash: [: 2: unary operator expected
wrong
[root@LAMP script]# [ 2 >= 3 ]  && echo right || echo wrong 
-bash: [: 2: unary operator expected
wrong
[root@LAMP script]# [[ 2 \<= 3 ]]  && echo right || echo wrong   
-bash: conditional binary operator expected
-bash: syntax error near `\<="
[root@LAMP script]# [[ 2 <= 3 ]]  && echo right || echo wrong 
-bash: syntax error in conditional expression
-bash: syntax error near `3"
[root@LAMP script]# [[ 2 >= 3 ]]  && echo right || echo wrong 
-bash: syntax error in conditional expression
-bash: syntax error near `3"

本文来源:http://www.bbyears.com/caozuoxitong/70902.html