break、continue、exit、return的区别和对比

一:说明

break、continue在条件循环语句及循环语句(for、while、if等)中用于控制程序的走向;而exit则用于种植所有语句并退出当前脚本,除此之外,exit还可以返回上一级程序或命令的执行状态值给当前shell;return类似于exit,只不过return仅用于在函数内部返回函数执行的状态值。

break n

如果省略n,则表示跳出整个循环,n表示跳出循环的层数;

continue n

如果省略n,则表示跳出本次循环,忽略本次循环的剩余代码,进入循环的下一次循环,n表示退到第n层继续循环;

exit n

退出当前shell程序,n为上一次程序执行的状态返回值。n也可以省略,在下一个shell里可以通过"$?"接收exit n的n值;

return

用于在函数里作为函数的返回值,以判断函数执行是否正确,在下一个shell里可以通过"$?"接收exit n的n值;

 

二、break、continue、exit功能执行流程图

这里以while循环和for循环为例来说明:

1、在循环中break功能的执行流程逻辑图如下:

for循环插入报错The last packet successfully received from the server was 270_for循环

2、在循环中continue功能的执行流程逻辑图:

for循环插入报错The last packet successfully received from the server was 270_执行流程_02

3、在循环中exit功能的执行流程逻辑图:

for循环插入报错The last packet successfully received from the server was 270_Code_03

 

三、break、continue、exit、return命令的示例:

说明:通过break命令跳出整个循环,执行循环下面的其他程序。

#!/bin/bash

#通过break命令跳出整个循环,执行循环下面的其他程序;

for循环插入报错The last packet successfully received from the server was 270_Code_04

for循环插入报错The last packet successfully received from the server was 270_Code_05

1 if [ $# -ne 1 ];then    #如果传参个数不为1,则打印下面的使用提示给用户;
2 
3     echo $"usage:$0 {break|continue|exit|return}"    #分别传入4个命令作为参数;
4 
5     exit 1    #退出脚本
6 
7 fi

View Code

#定义测试函数;

for循环插入报错The last packet successfully received from the server was 270_Code_04

for循环插入报错The last packet successfully received from the server was 270_Code_05

1 test(){
 2 
 3     for((i=0; i<=5; i++))
 4 
 5     do
 6 
 7         if [ $i -eq 3 ] ;then
 8 
 9             $*;        #$*是接收函数外的参数,将来就是{break|continue|exit|return}中的一个;
10 
11         fi
12 
13         echo $i
14 
15     done
16 
17     echo "I am in func."
18 
19 }
20 
21  
22 
23 test $*    #这里的$*为函数的传参;
24 
25 func_ret=$?    #接收并测试函数返回值;
26 
27  
28 
29 if [ `echo $*|grep return|wc -l` -eq 1 ]    #如果传参有return;
30 
31     then    
32 
33         echo "return's exit status:$func_ret"    #则提示return退出状态;
34 
35 fi
36 
37 echo "ok"        #函数外的输出提示;

View Code

传入break命令的执行结果为:

for循环插入报错The last packet successfully received from the server was 270_Code_04

for循环插入报错The last packet successfully received from the server was 270_Code_05

1 [root@shell ~]# sh 12-1-1.sh
 2 
 3 usage:12-1-1.sh {break|continue|exit|return}
 4 
 5 [root@shell ~]# sh 12-1-1.sh break
 6 
 7 0
 8 
 9 1
10 
11 2
12 
13 I am in func.
14 
15 ok

View Code

说明:i等于3及以后的循环没有被执行,但循环外的echo执行了,执行到break时跳出了if及外层的for循环语句,然后执行for循环外部done后面的的打印OK的语句。

 传入continue命令的执行结果为:

for循环插入报错The last packet successfully received from the server was 270_Code_04

for循环插入报错The last packet successfully received from the server was 270_Code_05

1 [root@shell ~]# sh 12-1-1.sh continue
 2 
 3 0
 4 
 5 1
 6 
 7 2
 8 
 9 4
10 
11 5
12 
13 I am in func.
14 
15 ok

View Code

说明:只有i等于3这层循环没有被执行,其他循环全部执行了,循环外的echo也执行了,说明执行到continue时,终止了本次循环,而继续下一次的循环,直到循环正常结束,接着继续执行了循环外外面的所有语句。

3传入exit 119命令的执行结果为:

for循环插入报错The last packet successfully received from the server was 270_Code_04

for循环插入报错The last packet successfully received from the server was 270_Code_05

1 [root@shell ~]# sh 12-1-1.sh "exit 119"
 2 
 3 0
 4 
 5 1
 6 
 7 2
 8 
 9 [root@shell ~]# echo $?
10 
11 119

View Code

说明:当进入循环里的if语句后遇到"exit 119"时,立刻退出程序,不但循环体3后面的数字没有输出,而for循环体done外面的echo函数和函数外的OK也没有输出,就直接退出了程序。另外,因程序退出指定了119,所以执行脚本后获取"echo $?"的返回值就返回了"exit 119"后面的119这个数字到当前的shell。

传入return 119命令的执行结果为:

for循环插入报错The last packet successfully received from the server was 270_Code_04

for循环插入报错The last packet successfully received from the server was 270_Code_05

1 [root@shell ~]# sh 12-1-1.sh "return 119"
 2 
 3 0
 4 
 5 1
 6 
 7 2
 8 
 9 return's exit status:119
10 
11 ok
12 
13 [root@shell ~]# echo $?
14 
15 0

View Code

说明:当进入循环里的if语句后遇到return 119,就没有打印3以下的数字,说明return跳出了循环体,程序也没有执行for循环体done外的echo命令,而是直接执行了函数test外的if语句及打印OK的命令,可见return的作用是退出当前函数。同时,return将数字119作为函数的执行状态值返还给函数体外,执行脚本后打印返回值是0,因为程序的最后一行是打印OK的命令,执行时成功的。

 

******************************我也想难过的时候到海边走走,可是我的城市没有海。******************************