Conditional judgment----Use if ... then----multiple
In the judgment of the same data, what should be done if the data needs to be judged in many different ways? For example, in the sh06.sh script above, we just have to make a $yn judgment (only once if) and don't want to make multiple if judgments. At this point you need to know the following syntax:
# A conditional judgment, sub success and failure (else)
If [conditional judgment]; Then
When the conditional judgment is established, the instruction work content can be carried out;
Else
When the conditional judgment is not established, the instruction work content can be carried out;
Fi
If you consider a more complex situation, you can use this syntax:
# Multiple conditional judgments (if ... else) are performed in a number of different situations (if elif ... elif)
If [conditional judgment type one]; Then
When the conditional judgment is established, the instruction work content can be carried out;
elif [conditional judgment type II]; Then
When the conditional judgment two is established, the instruction work content can be carried out;
Else
When the conditional judgment one and two are not established, the instruction work content can be carried out;
Fi
What you have to note is that Elif is also a judgment, so the appearance of elif will be followed then to deal with! But else is the last result of the failure, so else there is no then!
The source program is as follows:
1#!/bin/Bash2 #..........3Echo"test start or stop"4 #test Services5Var_ftp= '/usr/bin/pgrep" $"`6 if["$var _ftp"!="" ];7 Then8Echo"Service is running!"9 ElseTenEcho"service is not running!" OneFi
Create the shell source program with the VI editor:
Enter (enter) to write the shell source program with the VI editor:
The results of the implementation are as follows:
Shell programming Conditional----Use if ... then----multiple