1. Description
break (loop control), continue (circulation control), exit (exit the script), return (exit function)
Which break, continue in conditional statements and loop statements (for, while, if, etc.) control program to
which the execution state of exit and exit for all statements in the script, exit can also return to the previous program or command value to the current shell
which is similar return to exit, but only to return inside the function execution function returns a status value.
"N" is explained
command | Explanation |
---|---|
break n | N represents an out without the entire cycle, and n represents the number of layers out of the cycle |
continue n | N represents without skip this cycle, ignoring the remaining codebook cycle, the next cycle into the circulation. n represents the n-th layer retreated continue to cycle |
exit n | Exit the current shell program, n is the status of the last execution of the return value. n may be omitted, the shell next by "¥?" n receives the exit of the plant |
return n | Used in the function as the function's return value to determine whether the function is executed correctly. The shell can receive the next exit n by n-plant "$?" |
2, a flowchart
Break function flowchart is executed
while-break
for-break
continue functional flow diagram
while-continue
for-continue
functional flow exit
while-exit
for-exit
3, basic example
script
#!/bin/bash
if [[ $# -ne 1 ]]; then #<==如果传参数个数不为1,则打印下面的使用提示。
#statements
echo $"usage:0 {break|continue|exit|return}" #<==分别传入4个命令作为参数
exit 1 #<==退出脚本
fi
test(){ #<==定义测试函数
for (( i = 0; i <= 5; i++ )); do
#statements
if [[ $i -eq 3 ]]; then
#statements
$*; #<==这个地方的"$*"就是接收函数外的参数将来就是{break|continue|exit|return}中的一个
fi
echo $i
done
echo "I am in func." #<==循环外的输出提示
}
test $* #<==这里的”$*“为函数的传参
func_ret=$? #<==接收并测试函数返回值
if [[ `echo $*|grep return|wc -l` -eq 1 ]]; then #<==如果传参有return
#statements
echo "return's exit status:$func_ret" #<==提示return退出状态
fi
echo "ok" #<==函数外的输出提示
result
Incoming break command results
[root@test1 test]# ./break1.sh
usage:0 {break|continue|exit|return}
[root@test1 test]# ./break1.sh break
0
1
2
I am in func. #<==循环外的输出提示
ok #<==函数外的输出提示
See, I equals 3 and subsequent cycles is not performed, but the echo is performed outside the loop, it is performed to break out and if for outer loop, and then perform printing outside ok done statement after the for loop
Incoming continue command results
[root@test1 test]# ./break1.sh continue
0
1
2 #<==没有3
4
5
I am in func. #<==循环外的输出提示
ok #<==函数外的输出提示
I can see only equal to 3 this layer is not executed, the other is performed, echo also performed outside the loop, this is the only explanation continue the termination of the "3" This cycle
Incoming exit command
[root@test1 test]# ./break1.sh exit
0
1
2
To exit the shell
Incoming return command
[root@test1 test]# ./break1.sh return
0
1
2
return's exit status:0
ok
We can see the exit function, and prompting return exit status
Incoming exit 118 and return 118
[root@test1 test]# ./break1.sh "exit 118"
0
1
2 #<==只打印了0,1,2
[root@test1 test]# echo $?
118
[root@test1 test]# ./break1.sh "return 118"
0
1
2
return's exit status:118 #<==确实将118返回到了函数的外部脚本
ok
[root@test1 test]# echo $?
0 #<==执行脚本后的返回值还是0
[root@test1 test]#
Exit 118 can be seen to exit the loop, there is no ok output external function, the return value of the specified 118
can see the return 118, the function exits and returns the output value 118, and the output external function ok