The structure of the if statement
In shell scripts, if a statement is most commonly used in process control. The result is used to determine specific conditions, different operations (if ... then ....) respectively. Depending on the complexity, selection structure if statement can be divided into three basic types, suitable for different applications.
1. Single-branch if statement
Only when 'conditions are met' will execute the appropriate code, or do nothing.
Format: if a conditional test operation
then (the establishment of the conditions will be performed)
Sequence of commands
The end of the fi
Process: with back if conditional, only two results "established or not established." Analyzing conditions established, then executes subsequent commands in a sequence, it will not perform the end is not established directly fi.
2. if the bifurcation structure
Dual branch selection structure is required for "condition is satisfied," "condition does not hold" the two cases respectively perform different operations
Format: if
then the command sequence 1 (condition is satisfied)
else command sequence 2 (condition is not satisfied)
be
Process: if the judgment condition is satisfied, on the implementation of the conditions set up "then" Command 1, conditions are not established to perform "else" command sequence 2.
3. if the multi-branch structure
Since the if statement may be established according to the test results, are not met perform an operation, it is possible to nest, multiple determination.
Format: if test operation conditions 1
then the command sequence 1
2, which test elif
then the command sequence 2
else
Command sequence 3
be
Process: first determine if the condition is satisfied, if not established, in no hurry to exit, go to judgment "elif" conditions are satisfied, if set up on the implementation of "then" command sequence 2, if not established, it defaults to "else" of 3 command sequence.
)