This article mainly introduced in the shell for And while loop detailed summary, this article explained for the For Loop's digital section form, detailed lists, to the file circulation, while loop's three kinds of use situation and so on content, needs the friend to be possible to refer to under
One, for loop
1. Digital Segment Form
The code is as follows:
For i in {1..10}
Todo
Echo $i
Done
2. Detailed list (character and number of items are not many)
The code is as follows:
For File in 1 2 3 4 5
Todo
Echo $File
Done
3. Circulation of existing documents
Copy code code as follows:
For shname in ' LS *.sh '
Todo
Name= ' echo ' $shname | Awk-f. ' {print '} '
Echo $name
Done
4. Lookup loop (LS data too large can also use this method)
The code is as follows:
For Shname in ' find. -type f-name "*.sh"
Todo
Name= ' echo ' $shname | awk-f/' {print $} '
Echo $name
Done
5. (Grammar loop--a bit like C syntax, but remember double bracket
The code is as follows:
For ((i=1;i<100;i++))
Todo
if ((i%3==0))
Then
Echo $i
Continue
Fi
Done
6.seq form starting from 1
The code is as follows:
For i in ' SEQ 100 '
Todo
if ((i%3==0))
Then
Echo $i
Continue
Fi
Done
Two, while loops
1.while Circular note for square brackets [], and note that spaces
The code is as follows:
Min=1
max=100
While [$min-le $max]
Todo
Echo $min
min= ' expr $min + 1 '
Done
2. Double bracket form, internal structure somewhat like C syntax, note assignment: i=$ (($i + 1))
The code is as follows:
I=1
while (($i <100))
Todo
if (($i%4==0))
Then
Echo $i
Fi
i=$ (($i + 1))
Done
3. Read from the configuration file and can control the number of processes
The code is as follows:
Max_run_num=8
Cat cfg/res_card_partition.cfg |grep-v ' ^$ ' |grep-v ' # ' | grep-v grep |while Read partition
Todo
Nohup sh inv_res_card_process.sh $partition >log/rescard$partition.log 2>&1 &
While [1-eq 1]
Todo
Psnum= ' Ps-ef | grep "Inv_res_card_process" | Grep-v "grep" | Wc-l '
If [$psNum-ge $MAX _run_num]
Then
Sleep 5
Else
Break
Fi
Done
Done
Three. Loop control statement
The code is as follows:
# Break command does not execute the current loop body break the following statement exits from the current loop.
# Continue command is the procedure to ignore the following statements in this body, starting from the loop header