Linux Gvim shell break和continue命令

Posted Blog4Matto

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux Gvim shell break和continue命令相关的知识,希望对你有一定的参考价值。

#break和continue命令:循环过程中,有时需要在未达到循环结束条件时强制跳出循环
#break:允许跳出所有循环(终止执行后面的所有循环)

 1 while :        #死循环写法#
 2 do
 3     echo -n " Input a number between 1 to 5 : "
 4     read aNum
 5     case ${aNum} in
 6     1|2|3|4|5) 
 7         echo " Your number is ${aNum}"
 8         ;;
 9     *) 
10         echo " You do not select a number betwwen 1 to 5 , game over ! " 
11         break
12         ;;
13     esac
14 done

#循环嵌套,break可以跟一个参数,表示跳出第几层循环
#例:如果var1=2并且var2=0,跳出循环

for var1 in 1 2 3
do
    for var2 in 0 5 
    do
        if [ ${var1} -eq 2 -a ${var2} -eq 0 ]
        then 
            break 2
        else
            echo " ${var1} ${var2} "
        fi
    done
done

#continue命令:不会跳出所有循环,仅仅跳出当前循环
#运行代码发现,输入的不是1-5的数字,循环也不会被终止。echo " game over " 永远不被执行

 1 while :
 2 do
 3     echo " Input a number between 1 to 5 "
 4     read aNum
 5     case ${aNum} in
 6     1|2|3|4|5)
 7         echo " Your number is ${aNum} "
 8         ;;
 9     *)
10         echo " you do not select a number between 1 to 5 ! "
11         continue
12         echo " game over "
13         ;;
14     esac
15 done

#寻找偶数

 1 numbers="1 2 3 4 5 6"
 2 for NUM in ${numbers}
 3 do
 4     Q=` expr ${NUM} % 2 `
 5     if [ ${Q} -eq 0 ]
 6     then
 7         echo " Number is an even number "
 8         continue
 9     fi
10     echo " Found odd number "
11 done

 

以上是关于Linux Gvim shell break和continue命令的主要内容,如果未能解决你的问题,请参考以下文章

Linux Gvim shell 变量

Linux Gvim 创建第一个shell脚本

Linux Gvim shell 函数

Linux Gvim shell while循环

Linux Gvim shell if...else语句

Linux Gvim shell until需弩换