One, single branch if statement
Format: if judge condition; then
Statement1
statement2 ...
Fi
Two, two-branched if statement
Format: if judgment condition; Then
Statement1
statement2 ...
else
statementn
...
Fi
Use a previously used script to illustrate this structure.
Three, multiple branch if statement
Format: If Judge condition 1;then
Statement1 ...
Elif judgment Condition 2; Then
statement2 ...
Elif judgment Condition 3; Then
statement3 ...
else
statement4
...
Fi
Iv. Case Statement
Format: Case variable in
PATTERN1)
statement
...
;
PATTERN2)
statement
...
;
*)
statement
...
;
; Esac
Because the judgment structure statement is relatively simple, did not do too much explanation, just remembers each statement the structural usage can.
Supplemental Shell Basic Syntax
1.1, Shell file at the beginning
The shell file must start with the following line (must be on the first line of the file):
#!/bin/sh
The symbolic #! is used to tell the system that the parameters behind it are the programs used to execute the file. In this example we use/BIN/SH to execute the program.
When you edit a good script, you must also make it executable if you want to execute the script.
To make the script executable:
Run chmod +x filename so you can use./filename to run
1.2 Notes
In shell programming, comments are expressed in sentences that begin with # until the end of the line. We sincerely recommend that you use annotations in your programs.
If you use annotations, you can understand how the script works and work in a very short period of time, even if you haven't used it for a very long time.
1.3 Variables
In shell programming, all variables are composed of strings, and you do not need to declare the variables, direct assignment can be, the use of variables, the form of $+ variable names.
To assign a value to a variable, you can write this:
Now print the contents of variable a:
Sometimes variable names can easily be confused with other words, such as:
This does not print out "This is the 2nd" and only prints "This is the" because the shell searches for the value of the variable numnd, but the variable has no value. You can use curly braces to tell the shell what we want to print is the NUM variable:
This will print: This is the 2nd
1.4 Environment variables
Variables processed by the EXPORT keyword are called environment variables. We do not discuss environment variables, as we typically simply log in
Use environment variables in scripts.