If statement
1. What is a conditional test operation?
To make a shell script "intelligent", the problem is to differentiate between different situations to determine what action to take. The shell environment, based on the return status value ($?) after the command executes, determines whether the execution succeeds, and when the return value is 0 o'clock, the execution succeeds, otherwise it indicates a failure or an exception. You can test specific conditions by using the tool-test command for the conditional test action.
2. Condition Test classification
1) file test: Determine whether the file or directory is based on the given path.
Common options:
-D: Test for Directory
-F: Test for file
-E: Test whether the file or directory exists
-R: Tests whether the current user has Read permissions
-W: Tests whether the current user has write permissions
-X: Tests whether the current user has Execute permissions
2) Integer value comparison: The integer value comparison is based on two given integer values, judging the relationship between the first number and the second number
Common options:
-eq: Equals
-LT: Less than
-GT: Greater Than
-ne: Not equal to
-ge: greater than or equal to
-le: Less than or equal to
3) string comparison: Character creation comparisons are typically used to check user input, system conditions, and so on. In a shell environment that is interactive, it can also be used to determine if the location parameter entered by the user is in compliance with the requirements
Common operations:
=: The first character is the same as the second one
! =: The first is not the same as the second character
-Z: Check if character creation is empty
4) Logic test: Determine the dependency between two conditions or multiple conditions
&&: Indicates "and", the return value is 0 only if the first and the two conditions are true
| | : means "or", as long as one of the two conditions is true, the return value is 0
! : Indicates "no"
3. If statement
1) Single Branch if statement
If condition test action
Then
Command sequence
Fi
2) Dual-branch if statement
If condition test action
Then
Command sequence 1
Else
Command Sequence 2
Fi
3) Multi-branch if statement
If condition test action
Then
Command sequence 1
elif Condition Test Action 2
Then
Command Sequence 2
。。。
Else
Command sequence
Fi
4. Application examples
1) Create a new script to test the score
2) script
3) test Script
4) Script Interpretation
First condition Test action: When the input score is greater than or equal to 90, less than or equal to 100, the output "excellent"; Read-p is used to assign a value to the "GRADE" variable.
Shell script (2)-if statement