164 lines
2.1 KiB
Markdown
164 lines
2.1 KiB
Markdown
|
### 概览
|
||
|
- 列表循环:用于将一组命令执行已知的次数
|
||
|
- 不带列表循环,执行时由用户指定参数和参数的个数
|
||
|
- 类C风格循环
|
||
|
- break continue exit
|
||
|
-
|
||
|
### 列表循环
|
||
|
- 方式一
|
||
|
```shell
|
||
|
# 无步长
|
||
|
for variable in {list}
|
||
|
do
|
||
|
command1
|
||
|
command2
|
||
|
...
|
||
|
done
|
||
|
|
||
|
# 有步长
|
||
|
for variable in {list..步长}
|
||
|
do
|
||
|
command1
|
||
|
command2
|
||
|
...
|
||
|
done
|
||
|
|
||
|
```
|
||
|
|
||
|
- 方式二
|
||
|
```
|
||
|
for variable in a b c
|
||
|
do
|
||
|
command1
|
||
|
command2
|
||
|
...
|
||
|
done
|
||
|
```
|
||
|
- 示例
|
||
|
```
|
||
|
#! /bin/env bash
|
||
|
|
||
|
# 步长为2
|
||
|
for i in {1..5..2}
|
||
|
do
|
||
|
echo $i
|
||
|
done
|
||
|
|
||
|
#! /bin/env bash
|
||
|
for i in 1 2 3 4 5
|
||
|
do
|
||
|
echo $i
|
||
|
done
|
||
|
```
|
||
|
|
||
|
### 不带列表循环
|
||
|
- 方式一
|
||
|
```
|
||
|
for variable
|
||
|
do
|
||
|
command
|
||
|
command
|
||
|
...
|
||
|
done
|
||
|
```
|
||
|
|
||
|
- 示例
|
||
|
```
|
||
|
#! /bin/env bash
|
||
|
|
||
|
for i
|
||
|
do
|
||
|
echo hello
|
||
|
done
|
||
|
```
|
||
|
- 自动用$@ 用户输入的所有参数进行循环
|
||
|
```
|
||
|
me@me-EQ59:~/shell_demo/scripts/shell01$ bash -x for_not_list.sh a b c
|
||
|
+ for i in "$@"
|
||
|
+ echo hello
|
||
|
hello
|
||
|
+ for i in "$@"
|
||
|
+ echo hello
|
||
|
hello
|
||
|
+ for i in "$@"
|
||
|
+ echo hello
|
||
|
hello
|
||
|
me@me-EQ59:~/shell_demo/scripts/shell01$
|
||
|
```
|
||
|
|
||
|
```
|
||
|
me@me-EQ59:~/shell_demo/scripts/shell01$ bash -x for_not_list.sh {1..5}
|
||
|
+ for i in "$@"
|
||
|
+ echo hello
|
||
|
hello
|
||
|
+ for i in "$@"
|
||
|
+ echo hello
|
||
|
hello
|
||
|
+ for i in "$@"
|
||
|
+ echo hello
|
||
|
hello
|
||
|
+ for i in "$@"
|
||
|
+ echo hello
|
||
|
hello
|
||
|
+ for i in "$@"
|
||
|
+ echo hello
|
||
|
hello
|
||
|
me@me-EQ59:~/shell_demo/scripts/shell01$
|
||
|
```
|
||
|
|
||
|
```
|
||
|
me@me-EQ59:~/shell_demo/scripts/shell01$ bash -x for_not_list.sh `seq 5`
|
||
|
+ for i in "$@"
|
||
|
+ echo hello
|
||
|
hello
|
||
|
+ for i in "$@"
|
||
|
+ echo hello
|
||
|
hello
|
||
|
+ for i in "$@"
|
||
|
+ echo hello
|
||
|
hello
|
||
|
+ for i in "$@"
|
||
|
+ echo hello
|
||
|
hello
|
||
|
+ for i in "$@"
|
||
|
+ echo hello
|
||
|
hello
|
||
|
me@me-EQ59:~/shell_demo/scripts/shell01$
|
||
|
```
|
||
|
|
||
|
### 类C风格循环
|
||
|
- 方式一
|
||
|
```
|
||
|
# expr1 定义变量并赋初值
|
||
|
# expr 决定是否进行循环
|
||
|
# expr3 决定循环变量如何改变,决定循环什么时候退出
|
||
|
for (( expr1;expr2;expr4 ))
|
||
|
do
|
||
|
command
|
||
|
command
|
||
|
...
|
||
|
done
|
||
|
```
|
||
|
|
||
|
- 示例
|
||
|
```
|
||
|
for (( i=1;i<=5;i++ ))
|
||
|
do
|
||
|
echo $i
|
||
|
done
|
||
|
```
|
||
|
|
||
|
```
|
||
|
me@me-EQ59:~/shell_demo/scripts/shell01$ for ((i=0;i<=5;i++));do echo $i;done
|
||
|
0
|
||
|
1
|
||
|
2
|
||
|
3
|
||
|
4
|
||
|
5
|
||
|
me@me-EQ59:~/shell_demo/scripts/shell01$ for ((i=0;i<=5;i++));do [ $((i%2)) -eq 0 ] && echo $i;done
|
||
|
0
|
||
|
2
|
||
|
4
|
||
|
me@me-EQ59:~/shell_demo/scripts/shell01$
|
||
|
```
|