A, Shell conditional statement (if usage)
If statement structure [IF/THEN/ELIF/ELSE/FI]
Copy Code code as follows:
If condition test statement
Then
Action
[Elif condition
Action
Else
Action
]
Fi
If you are not very clear about: conditional test statements, you can refer to: Linux shell logical operators, logical expression of the detailed
Shell commands, which can be separated by semicolons or by line breaks. If you want to write multiple commands on one line, you can pass "';" Segmentation.
Such as:
Copy Code code as follows:
[Chengmo@centos5 ~]$ a=5;if [[A-GT 4]]; then echo ' OK '; fi;
Ok
Example: (test.sh)
Copy Code code as follows:
#!/bin/sh
scores=40;
if [[$scores-GT 90]]; Then
echo "Very good!";
elif [[$scores-GT 80]]; Then
echo "good!";
elif [[$scores-GT 60]]; Then
echo "pass!";
Else
echo "No pass!";
Fi
The conditional tests are: [[]],[],test], note: [[]] separated from the variable by a space.
Second, the circular statement (For,while,until usage):
1.for Recycling Method (For/do/done)
Syntax structure:
Copy Code code as follows:
1.for.. in statement
For variable in SEQ string
Todo
Action
Done
Description: Seq string as long as the space character segmentation, each time for...in read, will be read in order to the value, to the previous variable.
Instance (testfor.sh):
Copy Code code as follows:
#!/bin/sh
For I in $ (seq 10); Todo
echo $i;
Done
Seq 10 produces 1 2 3 .... 10 space-delimited string.
2.for (assignment; condition; Operation statement)
Copy Code code as follows:
for ((assignment; condition; Operation statement)
Todo
Action
Done
Instance (testfor2.sh):
Copy Code code as follows:
#!/bin/sh
For ((i=1;i<=10;i++));d o
echo $i;
Done
3.while Recycling (While/do/done)
While statement structure
Copy Code code as follows:
While conditional statement
Todo
Action
Done
Example 1:
Copy Code code as follows:
#!/bin/sh
i=10;
while [[$i-GT 5]];d o
echo $i;
((i--));
Done
Run Result: ========================
Copy Code code as follows:
SH testwhile1.sh
10
9
8
7
6
Instance 2: (Looping through the contents of the file:)
Copy Code code as follows:
#!/bin/sh
While Read Line;do
Echo $line;
Done </etc/hosts;
Run Result: ===================
Copy Code code as follows:
SH testwhile2.sh
# do don't remove the following line, or various programs
# that require network functionality would fail.
127.0.0.1 centos5 localhost.localdomain localhost
4.until Loop statement
Syntax structure:
Copy Code code as follows:
Until conditions
Todo
Action
Done
Until the condition is met, exit. Otherwise, the action is executed.
Instance (testuntil.sh):
Copy Code code as follows:
#!/bin/sh
a=10;
until [[$a-lt 0]];d o
echo $a;
((a));
Done
Run Result:
SH testuntil.sh
Copy Code code as follows:
III. Shell Selection Statement (case, select usage)
1.case SELECT statement Use (CASE/ESAC)
Grammatical structure
Copy Code code as follows:
Case $arg in
Pattern | Sample) # arg in-pattern or sample
PATTERN1) # arg in PATTERN1
*) #default
Esac
Note: Pattern1 is a regular expression, you can use the following characters:
* Any string
? Any character
[ABC] A, B, or C-one of the three characters
[A-n] Any character from A to n
| Multiple selection
Instance:
Copy Code code as follows:
#!/bin/sh
Case is in
Start | Begin
echo "Start Something"
Stop | End
echo "Stop Something"
*)
echo "Ignorant"
Esac
Run Result: ======================
Copy Code code as follows:
testcase.sh start
Start something
2.select Statement Use Method (Generate menu selection)
Grammar:
Copy Code code as follows:
Select variable name in SEQ variable
Todo
Action
Done
Instance:
Copy Code code as follows:
#!/bin/sh
Select ch in "Begin" "End" "Exit"
Todo
Case $ch in
"Begin")
echo "Start Something"
"End")
echo "Stop Something"
"Exit")
echo "Exit"
Break
*)
echo "Ignorant"
Esac
Done
Run Result:
Note: Select is a circular selection, typically used with case statements.