zl程序教程

您现在的位置是:首页 >  Java

当前栏目

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

2023-02-18 16:46:48 时间

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

示例

#!/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"
}