One, the file operation
1. Expression
-D: Whether it is a directory
-F: Whether it is a file
-E: Whether the file or directory exists
-R: Whether the current user has Read permissions
-W: Whether the current user writes permissions
-X: Whether the current user has executable permissions
2. Example (using the test command at the command line or [])
[[email protected] ~]# test-d/home/oldcat/tools/[[email protected] ~]# echo $?0[[email protected] ~]# test-d/HOME/OLDC at/tools/redis-3.0.7.tar.gz [[email protected] ~]# echo $?1[[email protected] ~]# test-f/home/oldcat/tools/redis-3.0.7 . tar.gz [[email protected] ~]# echo $?0 Description: Output execution result is 0 true, reverse false
Second, numerical comparison
1. Expression
-EQ: Two operands are equal
-ne: Whether the two operands are not equal
-le: Whether the left operand is less than or equal to the right operand
-ge: Whether the left operand is greater than or equal to the right operand
-LT: Whether the left operand is less than the right operand
-GT: Whether the left operand is greater than the right operand
2. Examples and explanations
[[email protected] ~]# test 1-eq 2[[email protected] ~]# echo $?1[[email protected] ~]# test 1-eq 1[[email protected] ~] # echo $?0[[email protected] ~]# test 2-gt 1[[email protected] ~]# echo $?0[[email protected] ~]# test 1-ge 1[[email Pro Tected] ~]# echo $?0 Description: Output execution result is 0 true, reverse is false
Three, string comparison
1. Expression
=: two strings are equal
! =: Two field strings are not equal
-Z: Determines whether the field string is empty
2. Examples and explanations
[Email protected] ~]# Test "CHENFL" = "CHENFL" [[email protected] ~]# echo $?1[[email protected] ~]# test "CHENFL"! = "Che NFL "[[email protected] ~]# echo $?1[[email protected] ~]# test" CHENFL "=" CHENFL "[[email protected] ~]# echo $?0[[email p Rotected] ~]# test-z "" [[email protected] ~]# echo $?0[[email protected] ~]# test-z "CHENFL" [[email protected] ~]# Echo $?1 Description: Output execution result is 0 true, reverse is false
Iv. Logical Judgment
1. Expression
&&: Logic and, only the first and the two expressions are true execution result is true (0), otherwise false (1)
|| : Logical OR, before and after two expressions as long as one is true, the execution result is true (0), otherwise false (1)
!: logical non If the expression execution result is True then false (1), if the expression execution result is false that is true (0)
2. Examples and explanations
[[Email protected] ~]# [1-eq 1] && ["hehe"! = "hehe"][[email protected] ~]# echo $?1[[email protected] ~]# [ 1-eq 1] | | ["hehe"! = "hehe"] [[email protected] ~]# echo $?0[[email protected] ~]# [!-d/home/oldcat/tools/][[email protected] ~]# echo $?1 Description: Output execution result 0 is true, and the reverse is false
About conditional expressions in shell scripts