linux shell编程中 break和exit的区别

 

break是跳出循环
exit是退出脚本。

 

看下面的例子。

001、break

[root@PC1 test02]# cat test.txt        ## 测试数据
3
4
5
6
7
[root@PC1 test02]# cat test.sh         ## 测试程序
#!/bin/bash
for i in $(cat test.txt)
do
        if [ $i -gt 5 ]
        then
                break
        else
                echo "$i"
        fi
done
echo "the end"
[root@PC1 test02]# bash test.sh     ## break跳出循环,仍然执行后面的程序
3
4
5
the end

 

002、exit

[root@PC1 test02]# cat test.txt             ## 测试数据
3
4
5
6
7
[root@PC1 test02]# cat test.sh              ## 测试程序
#!/bin/bash
for i in $(cat test.txt)
do
        if [ $i -gt 5 ]
        then
                exit               ## exit表示退出程序,不再执行后面的程序
        else
                echo "$i"
        fi
done
echo "the end"
[root@PC1 test02]# bash test.sh
3
4
5

 

 

posted @ 2023-07-10 15:45  小鲨鱼2018  阅读(130)  评论(0)    收藏  举报