一、Break 结束循环
????????Break 结束循环实例
[root@localhost ~]# awk 'BEGIN {
> sum=0; for(i=0;i<20;++i) {
> sum += i; if(sum>50) break; else print "Sum=",sum
> }
> }'
Sum= 0
Sum= 1
Sum= 3
Sum= 6
Sum= 10
Sum= 15
Sum= 21
Sum= 28
Sum= 36
Sum= 45
?
?二、Continue 跳出本次循环
????????Continue 跳出本次循环实例
[root@localhost ~]# awk 'BEGIN {for (i=1;i<=20;++i) {if (i % 2 ==0) print i; else continue}}'
2
4
6
8
10
12
14
16
18
20
?
?三、Exit 结束脚本程序
????????Exit 结束脚本程序实例
[root@localhost ~]# awk 'BEGIN {
> sum=0; for(i=0;i<20;++i) {
> sum +=i;if (sum > 50) exit(10); else print "Sum =",sum
> }
> }'
Sum = 0
Sum = 1
Sum = 3
Sum = 6
Sum = 10
Sum = 15
Sum = 21
Sum = 28
Sum = 36
Sum = 45
[root@localhost ~]# echo $? #检查脚本执行后的返回状态
10
?
?四、Next 停止处理
????????Next 停止处理实例
[root@localhost ~]# awk -F: '{if($3%2!=0) next; print $1,$3}' /etc/passwd
root 0
daemon 2
lp 4
shutdown 6
mail 8
games 12
ftp 14
systemd-network 192
sshd 74
chrony 998
apache 48
tom 1000
Tom 1002
Jack 1004
opp 1008
qian 1010
huang 1012
kong 1014
duo 1016
ji 1018
han 1020
?
?