Shell commands can be separated by semicolons or line breaks. If you want to write multiple commands in one line, you can use "';" to separate them, for example:
Example: (test. sh)
Ii. Loop statements (for, while, until usage ):(1) for Loop usage (for/do/done)
1.... In statement-syntax structure
For variable in seq string # As long as the seq string is separated by space characters, each time... When reading in, the read value is given to the preceding variable in order. Doactiondone
Instance (testfor. sh ):
#! /Bin/shfor I in $ (seq 10); do # seq 10 generates 1 2 3 ...... 10-space separator string echo $ I; done;
2. for (assign value; condition; Operation statement ))
For (assign value; condition; Operation statement) doactiondone;
Instance (testfor2.sh ):
#!/bin/shfor((i=1;i<=10;i++));doecho $i;done;
(2) while loop usage (while/do/done)
While Condition Statement doactiondone;
Instance 1:
#!/bin/shi=10;while [[ $i -gt 5 ]];doecho $i;((i--));done;
Running result:
sh testwhile1.sh109876
Example 2: (Read File Content cyclically :)
#!/bin/shwhile read line;doecho $line;done < /etc/hosts;
Running result:
sh testwhile2.sh# Do not remove the following line, or various programs# that require network functionality will fail.127.0.0.1 centos5 localhost.localdomain localhost
(3) until loop statement-syntax structure
Until condition # exit after the condition is met. Otherwise, execute action. doactiondone.
Instance (testuntil. sh ):
#!/bin/sha=10;until [[ $a -lt 0 ]];doecho $a;((a—));done;
Result:
sh testuntil.sh109876543210
Iii. shell selection statement (case and select usage)(1) Use case/esac statements -- syntax structure
case $arg inpattern | sample) # arg in pattern or sample;;pattern1) # arg in pattern1;;*) #default;;esac
Note: pattern1 is a regular expression, which can contain the following characters:
* Any string? Any character
[Abc] a, B, or c
[A-n] any character from a to n
| Multiple options
Instance:
#!/bin/shcase $1 instart | begin)echo "start something";;stop | end)echo "stop something";;*)echo "Ignorant";;esac
Running result:
testcase.sh startstart something
(2) How to Use the select statement (generate menu selection) -- syntax
Select variable name in seq variable doactiondone
Instance:
#!/bin/shselect ch in "begin" "end" "exit"docase $ch in"begin")echo "start something";;"end")echo "stop something";;"exit")echo "exit"break;;;*)echo "Ignorant";;esacdone;
Running result:
From: http://www.cnblogs.com/chengmo/archive/2010/10/14/1851434.html
Address: http://www.linuxprobe.com/shell-process-control.html