总结几种 Shell 语言中可以用到的循环语句

for

数字循环

简单数字循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env bash
# Author: wxnacy([email protected])
# Description:

# function 1
# lists="1 2 3 4 5"
# for var in $lists

# function 2
# for var in 1 2 3 4 5

# function 3
for var in {1..5}
do
echo $var
done

命令行使用方式

1
$ for var in {1..5};do;echo $var; done

使用 seq

1
2
3
4
5
6
7
8
#!/usr/bin/env bash
# Author: wxnacy([email protected])
# Description:

for var in $(seq 1 10)
do
echo $var
done

使用 2 作为步长

1
2
3
4
5
6
7
8
#!/usr/bin/env bash
# Author: wxnacy([email protected])
# Description:

for var in $(seq 1 2 10)
do
echo $var
done

使用 c 语言的方式

1
2
3
4
5
6
7
8
#!/usr/bin/env bash
# Author: wxnacy([email protected])
# Description:

for ((var=1; var<=5; var ++))
do
echo $var
done

while

1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env bash
# Author: wxnacy([email protected])
# Description:

i=1

while(( i <= 10 ))
do
let "i+=1"
done
03-16 11:55