One, for loop
[email protected] shell]# cat for.sh
#!/bin/bash
For i in ' seq 1 10 '; Do
echo "$i"
Done
With this script you can see the basic structure of the For loop:
The condition of the for variable name in the loop; do Commanddone
[Email protected] shell]# sh for.sh
1
2
3
4
5
6
7
8
9
10
Example 2:
[email protected] shell]# cat for2.sh
#!/bin/bash
For a in ' LS '; Do
echo "$a"
Done
[Email protected] shell]# sh for2.sh
case1.sh
case.sh
for2.sh
for3.sh
for.sh
if1.sh
if.sh
Example 3
[email protected] shell]# cat for3.sh
#!/bin/bash
For file in ' Vmstat '; Do
echo "$file"
Done
For i in ' Cd/shell && ls '; Do
echo "$i"
Done
Reference system commands need to be inverted, others do not
[[email protected] shell]# for I in 1 4 5 3 A; Do echo "$i"; Done
1
4
5
3
A
A
Second, while loop
[email protected] shell]# cat while.sh
#!/bin/bash
A=6
While [$a-ge 1]; Do
Echo $a
a=$[$a-1]
Done
The while loop format is also simple:
While condition; Do Commanddone
[Email protected] shell]# sh while.sh
6
5
4
3
2
1
Example 2
[email protected] shell]# cat while2.sh
#!/bin/bash
While:; Do
Seq 1 3
Done
Replace the loop condition with a colon, so that you can do a dead loop
[Email protected] shell]# sh while2.sh
1
2
3
1
2
3
1
2
This article from "Unchanged Time---hu" blog, reprint please contact the author!
Shell's for, while loop