Shell Structures are divided into three categories: sequential structure, branch structure, and cyclic structure.
1. If statement
Syntax format:
If
<Statement sequence a>
Then
<Statement sequence B>
Fi
If the return value of the last command in statement sequence a is 0, execute the command in statement sequence B and continue executing the command after Fi; if the return value of the last command in statement sequence A is not 0, the system jumps to fi and continues executing the command after Fi,Note: here, the execution result of A is 0 and the result is true..
if test –f funfilethen echo funfile existsfi
if [ -f funfile ]; then echo funfile exists echo hellofi
2. If-else statement
Syntax format:
If
Statement sequence
Then
Statement sequence B
Else
Statement sequence C
Fi
If the return value of the last command in statement sequence a is 0, execute the command in statement sequence B and continue executing the command after Fi; if the return value of the last command in statement sequence A is not 0, execute the command in statement sequence C and continue executing the command after Fi.
The return values of any command can be used as judgment conditions or nested if statements, but the IF and Fi pairs must be ensured.
if [ “$X” –lt 10 ]; then echo X is less than 10else if [ “$X” –gt 10 ]; then echo X is greater than 10 else echo X is equal to 10 fifi
You can also use the abbreviated form: Elif to replace an else if nesting.
3. Case Structure
Syntax structure:
Case word in
Mode)
Statement sequence
;;
Mode B)
Statement sequence B
;;
...
Esac
Branch selection is to compare words in sequence with the provided mode. If they are consistent, execute the subsequent statement sequence, jump to esac, and execute subsequent statements. In typical cases, a word is a variable, and a pattern can be used ?, .
case “$1” in start) start ;; stop) stop ;; *) echo “usage: $0 {start|stop}” ;;esac
4. While statement
Syntax format:
While
Statement sequence
Do
Statement sequence B
Done
Statement function: execute commands in sequence A. If the return value of the last command in sequence a is 0, execute statement sequence B, return to step 1, and repeat the process above, the Return Value of the last command in statement sequence A is not 0.
x=1while ((x <= 10))do echo x is $x let x=x+1done
Here we will explain the let Command. Let will execute arithmetic operations in shell. The named parameter can be accessed directly using the name in the arithmetic expression without the "$" symbol. When a named parameter is accessed, its value is calculated as an arithmetic expression. The arithmetic expression is evaluated by a long integer without Overflow check. Of course, an error occurs when 0 is used as the divisor. The alternative representation of let is: (arithmetic expression ))
5. Until statement
Syntax structure:
Until
Statement sequence
Do
Statement sequence B
Done
The until and while statements are only opposite to the test conditions, and the others are the same.
6. For statement
Syntax structure
For VaR in list
Do
Statement sequence
Done
1. if the list contains a token, go to step 2; otherwise, the process ends. 2. vaR is set to the next token in the list; 3. the command in statement sequence a is executed; 4. return to Step 1
Note: here, the VaR update is replaced by the next variable in the list after each loop, which is different from the loop count in C.
echo file list:for i in *; do echo $idone
7. Continue and break statements
If you use continue in the loop body, the loop jumps out and enters the next loop. If you use break, the entire loop is exceeded.
8. Shell functions
A function is a script in a script. Using a function can greatly speed up calling than using a new script.
There are two function formats:
(1) function name
{
...
}
(2) function name ()
{
...
}
Calling a function is like calling a shell script or command. You can directly write the function name. If necessary, you can also follow the parameters. Shell search commands are ordered by aliases, keywords, functions, internal commands, scripts, and executable programs. Therefore, functions may block external scripts or programs.
You can pass parameters to a function. The processing in a function is basically the same as that in a common script. You can use location parameters such as $1 or $ *, $ #, etc, but $0 in the function does not represent the function name, but is still the script file name.
Start () {echo "first parameter: $1" echo "$0: $ *"}
Call
start a b c
Unless otherwise specified, the variables accessed in the function are the variables shared by the entire script. If you want to define a local variable that is only valid in the function, you need to use the local statement.