1. Cycle
The shell loops and executes a program continuously until the condition is met. Loops are divided into 4 types, while loops, until loops, for fixed processing, for numeric processing.
2. While loop
The while loop is executed until the condition is not met before stopping. Syntax: while [condition]doDo somethingdone example: Loop until user input is correct
#!/bin/bash# desc:while loopwhile ["$yn"! = "Yes"-a "$yn"! = "yes"] do read-p "Please input yes/yes to stop:" Yndoneecho "OK"
Execution Result:
Example: from 1 to 100
#!/bin/bash# desc:while loopsum=0i=0while [$i-lt]do i=$ (($i + 1));
3. Until cycle
Until loops and while instead, the loop is terminated when the condition condition is established. Syntax: until [condition]doDo Somethingdone Example:
#!/bin/bash# desc:while loopuntil ["$yn" = = "Yes"-o "$yn" = = "Yes"]do read-p "Please input yes/yes to stop:" Y Ndoneecho "OK"
Perform:
[Email protected] sh]$ sh while.sh Please input yes/yes to Stop:hi
4. For Fixed loop
For is a loop of known number of times. Syntax: for var in con1 Con2. DoDo Somethingdone Example:
#!/bin/bash# desc:for loopfor animal in dog cat pig does echo "HI, ${animal}" done
Perform:
[[Email protected] sh]$ sh for.sh HI, Doghi, Cathi, pig[[email protected] sh]$
Example: Current catalog file
#!/bin/bash# desc:for loopfilelist=$ (LS) for filename in $filelistdo echo $filenamedone
5. For-Value loops
For numeric loops, suitable for cyclic numerical calculations. Syntax for (initial value; limit value; step)) doDo Somethingdone Example:
#!/bin/bash# desc:for loopsum=0for ((i=0; i<=100; i++)) do sum=$ (($sum + $i)) Doneecho ' sum is: ' $sum
Address: http://blog.csdn.net/yonggang7/article/details/40679701
Shell Loop Loops