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

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

罗小黑
2021-07-03 / 0 评论 / 510 阅读 / 正在检测是否收录...
广告
温馨提示:
本文最后更新于2021年07月05日,已超过998天没有更新,若内容或图片失效,请留言反馈。

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


back

continue

exit
示例

#!/bin/bash
if [ $# -ne 1 ]; then
    echo "Usage:$0 {break|continue|exit|return}"
    exit 1
fi
test() {
    for ((i=0; i<=5; i++))
    do
      if [ $i -eq 3 ];then
      $*;
      fi
      echo $i
    done
      echo "6666"
}
test $*
func_ret=$?
      if [ `echo $*|grep return|wc -l` -eq 1 ];then
      echo "return's exit status:$func_ret"
      fi
echo "ok"
}

kqqh57j9.png

0

打赏


评论 (0)

取消