break和continue

break和continue都是用来控制循环结构的,其主要作用是停止循环
前者是完全终止,跳出循环
后者则只是跳出当次的循环,开始下一次循环

举例:

break

[root@lUsmwg108555 ~]# cat break 
#!/bin/bash
for i in a b c d 
do 
echo -n "$i "
	for j in `seq 10`
	do
		if [ "$j" == 3 ];then
	    break
		fi
	echo -n " $j "
	done
	echo
done

[root@lUsmwg108555 ~]# . /root/break 
a  1  2 
b  1  2 
c  1  2 
d  1  2 
[root@lUsmwg108555 ~]# 

continue

[root@lUsmwg108555 ~]# cat break 
#!/bin/bash
for i in a b c d 
do 
echo -n "$i "
	for j in `seq 10`
	do
		if [ "$j" == 3 ];then
	    continue
		fi
	echo -n " $j "
	done
	echo
done

[root@lUsmwg108555 ~]# . /root/break 
a  1  2  4  5  6  7  8  9  10 
b  1  2  4  5  6  7  8  9  10 
c  1  2  4  5  6  7  8  9  10 
d  1  2  4  5  6  7  8  9  10 
[root@lUsmwg108555 ~]# 
comments powered by Disqus