Conditional Judgment of Bash programming
Bash Programming condition judgment: determine if the prerequisites for subsequent operations are met
1. The common judgment type of condition judgment (bash endogenous):
Integer test
Character test
File test
2. Echo $? (Execution status return value):
0: correct
1-255: Error br>
True or false
3, logical operation:
True && false = False (not 0)
OR operation:
true | | true = True (0)
true | | False = True
False | | True = True
False | | False = False
Non-arithmetic:
! true = False
! False = True
4. How to do the tests in bash: Test command
Test comparison expression
[Test expression]: The expression must have a space at both ends; bash's endogenous command
[[Test expression]: ditto; bash scripting keywords
The execution result of any command can also be used as a condition for judging
5, bash conditions to determine the use if:
Single branch:
if condition; Then
Branch 1;
Fi
Dual Branch:
if condition; Then
branch 1;
Else
branch 2;
fi
Multi-branch:
if condition; Then
branch 1;
elif Condition 2; Then
branch 2;
elif Condition 3; Then
Branch 3;
... ..
Else
branch N;
fi
The command substitution character cannot be used as long as the command is used as a condition to refer to its status result (that is, execution succeeds or not), not the output of the command.
if you want to save the result of the command execution to a variable, you need to add the reverse quotation mark to the command.
Bash Programming: integer Testing
1, two Yuan test: (typically two numbers to compare)
NUM1 operator num2
operator:
-eq equals
-ne not equal to
-le: less than or equal to
-ge: greater than or equal to
-lt: less than [$num 1 -lt $num 2]
-gt: greater than [$num 1-gt $num 2]
2, bash Knowledge point script automatically exits
exit [n] N is a number that is not 0,1,127,255
Condition test:
BASH: Every command, execution state has a return value
Success: 0
Failure: not 0
$?:
The state return value of the script: the last command executed by the script:
Custom script State return value: Exit[n], get the return value with $?
Execution result of the reference command: Using ' command ' or ' (Command ')
The status result of the success or absence of the reference command: it must be executed directly. At this point, it is often necessary to redirect execution results to/dev/null
Combinatorial condition Testing: making logical operations on conditions
With: Condition 1&& condition 2
Condition 1 is false, the final result must be false, otherwise, condition 2 is not enforced
Condition 1 is true, the final condition result is determined by the following condition, so condition 2 must perform
Or: Condition 1| | Condition 2
Condition 1 is true, the final result must be true, otherwise, condition 2 is not enforced
Condition 1 is false, the final condition results in a subsequent condition, so condition 2 must be performed
Non -:! Conditions
With a priority greater than or, or a priority greater than the non-
Bash programming: Character testing, General quotes
Binocular:
>: Greater Than
<: Less than
! =, <>: Not equal to
= = equals, equivalent, variable needs to be quoted
=~: The left side is a string, and the right side is a pattern that determines whether the left-hand string can be matched by the pattern on the right: normally only used in [[]]
You can use the beginning and end of the line anchor in the pattern, but don't enclose the pattern in quotation marks
Monocular:
-N $stringVar: string is not empty, not empty is true, empty is false
-Z $stringVar: Whether the string is empty, empty is true, and not empty is false:
15. Learning progress and task of self-study--linux "bash programming condition judgment"