The result returned by the conditional expression is Boolean
It's 1, false is 0.
Expressions for conditional tests
[Expression]
[[Expression]]
Test expression
Commonly used for:
Monocular:
-e file Test files exist
-F file Test is a normal file
-D File Test Specifies whether the path is a directory
-r file test for write permission
-W file test for write permission
-X file Tests whether the files have permission to execute
-Z: Determines whether the specified variable has a value
-N: Test whether the character is empty and true
! : Logical Non-
Binocular:
= = Equal to True
! = Not equal to True
> is greater than true
< is less than true
-A: Logic and both are true
-O: Logic or one is true
Test expression
Expression is conditional
if test (expression is true)
if test! An expression is false
Test expression 1–a expression 22 expressions are true
Test expression 1–o Expression 22 expressions have a true
Morgan's Law
! (a-a B) =! A-o! B
! (a-o B) =! A-a! B
The logical relationship between commands
&&: Logic and, both true and true
If the first condition is true, a second condition needs to be judged
If the first condition is false, there is no need to judge the second condition and the result has been determined to be false.
|| : Logical OR, one true is True
When the first condition is true, the second condition is not judged, because the first expression has determined that the entire expression is true
Only go back and judge the second expression when the first expression is false.
Apply to Script:
This is a script to compare the size of two integers
#!/bin/SHRead-P"Pls input nums:"a B [-Z $a] | | [-Z $b] && { Echo "Pls Input Nums"Exit1}Expr$a +1&>/dev/NULLRETRAVL1=$?Expr$b +1&>/dev/NULLRETRAVL2=$?Test $RETRAVL 1-eq0-A $RETRAVL 2-eq0||{ Echo "Pls Input Nums"Exit2}[$a-lt $b] &&Echo "$a < $b"[$a-eq $b] &&Echo "$a = $b"[$a-GT $b] &&Echo "$a > $b"
[Email protected] scripts]# sh var.sh
Pls Input 2 nums:1
1 < 2
[Email protected] scripts]# sh var.sh
Pls Input 1 Nums:2
2 > 1
[Email protected] scripts]# sh var.sh
Pls Input 1 nums:1
1 = 1
[Email protected] scripts]# sh var.sh
Pls input Nums:a
Pls input Nums
[Email protected] scripts]# sh var.sh
Pls input Nums:1
Pls input Nums
[Email protected] scripts]# sh var.sh
Pls Input 2 3 nums:1
Conditional expressions in the shell