8.if statement
Syntax: (for one-way judgment branches)
If
List A
Then
List B
Fi
Example:
If
Test-s Funfile
Then
Echo Funfile exists
Fi
echo Hello
An If structure is a process control method based on the return value of a command. If the test command returns a value of 0, a specified list of commands is executed, and if the command returned with a value other than 0, the specified list of commands is ignored and not executed.
The example above illustrates a common format for the IF structure: Each command list consists of a shell command of one or more UNIX systems, each of which is separated by a carriage return or semicolon, and the last command executed in List a determines the result of the IF statement.
The procedure for if structure execution is as follows:
1.list a command is executed.
2. If the last command in List a returns a value of 0 (True), execute the command in List B, and then continue with the command after FI.
3. If the return value of the last command in List A is not 0 (false), skip to fi and continue with the command after FI.
The test command is often used as a process control, and it can use any UNIX command because all UNIX commands produce a return value, as the following example illustrates:
If
grep kingkong/etc/passwd >/dev/http://www.aliyun.com/zixun/aggregation/19527.html ">null
Then
Echo found KingKong
Fi
The If structure can also provide process control when a program fails. As shown in the following example:
If
[$# NE 3]
Then
echo Incorrect syntax
echo usage:cmd arg1 arg2 Arg3
Exit 99
Fi