break continue exit return 的区别
[root@localhost day1]# cat ss.sh
#!/bin/bash
for ((i=0;i<5;i++))
do
if [ $i -eq 3 ]
then
break
#continue
#exit
#return
fii
echo $i;sleep 1
done
echo "ok"
###################################################
[root@localhost day1]# sh ss.sh #由此看出break是直接终断循环的
0
1
2
ok
——————————————————————————————
[root@localhost day1]# sh ss.sh #continue效果 还是中断当前循环,继续进行下一循环
0
1
2
4
ok
——————————————————————————————
[root@localhost day1]# sh ss.sh #exit 直接退出了脚本
0
1
2
return :return must be function
1.中断循环
2.返回值
[root@localhost day1]# sh return.sh
0
1
2
return.sh: line 12: return: can only `return' from a function or sourced script #必须是函数
3
4
ok
#!/bin/bash
fuction_return (){ #加入函数
for ((i=0;i<5;i++))
do
if [ $i -eq 3 ]
then
#break
#continue
#exit
return
fi
echo $i;sleep 1
done
echo "ok"
}
fuction_return
[root@localhost day1]# sh return.sh #结果
0
1
2