Test Condition Expression Description
File symbols:
[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 is unary or binary. Unary
Expressions are often used to examine the status of a file. There
are string operators as, 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 is 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:
The-o option True if the shell 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 a invalid argument is given.
-A file
True if file exists.
-B File
True if file exists and is a block device.
-C file
True if file exists and is a character device.
-D File
True if file exists and is a directory.
-E File
True if file exists.
-F File
True if file exists and is normal.
-G file
True if file exists and is the set group ID (sgid).
-H file
True if file exists and is a symbolic link.
-K File
True if file exists and the ' sticky ' bit (sticky bit) is set.
-P File
True if file exists and is a named pipe (FIFO).
-R File
True if file exists and is readable.
-S file
True if file exists and the size is greater than zero.
-T FD if the file descriptor FD is open and a corresponding terminal is true.
-U file
True if file exists and is set to the user ID (suid).
-W File
True if file exists and is writable.
-X File
True if file exists and is executable.
-O File
True if file exists and is owned by a valid user ID.
-G file
True if file exists and is owned by a valid group ID.
-L File
True if file exists and is a symbolic link.
-S file
True if file exists and is a socket.
-N File
True if file exists and has been modified since it was last read.
File1-nt File2
True if File1 is newer than file2 (based on modified date), or if file1 exists and file2 does not exist.
File1-ot File2
True if the file1 is older than file2, or if file1 does not exist and file2 exists.
File1-ef File2
True if File1 and File2 refer to the same device and inode number.
-O optname
True if the shell option is enabled optname. See the list of options in the description of the-O option under the built-in command set.
-Z String
True if string has a length of 0.
-N String
String if the length of string is not 0 is true.
string1 = = string2
True if the string is equal. = can be used to compatibility with the POSIX specification on the occasion of = =.
String1!= string2
True if the string is not equal.
String1 < string2
True if string1 is before string2 in the dictionary order of the current locale.
String1 > string2
True if string1 is after string2 in the dictionary order of the current locale.
Arg1 OP arg2
OP is one of-eq,-ne,-lt,-le,-GT, or-ge. These arithmetic binary operations return TRUE if the arg1 and arg2 are equal, unequal, less than, less than or equal to, greater than, greater than or equal to the relationship. Arg1 and
ARG2 can be positive/negative integers.
Shell conditional expression [] Description
Equal: Use-eq in [], in (()) [[]] Use = =
Not-equal: Use-ne in [], use!= in (()) [[]] Less-than: Use-LT in [], use in (()) [[]]
Less-than-or-equal: Use-le in [], use <= in (()) [[]] Greater-than: Use-GT in [], use in (()) [[]];
Greater-than-or-equal: Use-ge in [], use >= in (()) [[]],
logical operators:
and: Use-a in [], use && in (()) [[]];
or: in [] Using-O, in (()) [[]] ;
!: used in [], in (()) [[]]!;
Test: =,!= test
[Root@lamp script]# [2 = 2] && echo Right | | echo Wrong #如果2 = 2, then print right, otherwise wrong
Right
[Root@lamp script]# [2!= 2] && echo Right | | echo Wrong #如果2 ≠2, the right is printed, otherwise wrong
Wrong
[Root@lamp script]# [2 = 3] && echo Right | | echo Wrong #如果2 = 3, then print right, otherwise 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
Test:<, > Test
[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
####################### #特殊备注 ########################
Test: <=, >= test
[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 '