Shell也使用 break 和 continue 来跳出循环。

break命令

下面的例子中,脚本进入死循环直至用户输入数字大于5,使用break跳出这个循环。

#!/bin/bash
while :
do
echo -n "Input a number between 1 to 5: "
read aNum
case $aNum in
||||) echo "Your number is $aNum!"
;;
*) echo "You do not select a number between 1 to 5, game is over!"
break
;;
esac
done

在嵌套循环中,break 命令后面还可以跟一个整数,表示跳出第几层循环。

break n

下面是一个嵌套循环的例子,如果 var1 等于 2,并且 var2 等于 0,就跳出循环:

#!/bin/bash
for var1 in
do
for var2 in
do
if [ $var1 -eq -a $var2 -eq ]
then
break
else
echo "$var1 $var2"
fi
done
done

continue命令

continue命令与break命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环。

同样,continue 后面也可以跟一个数字,表示跳出第几层循环。

Shell脚本之:退出循环的更多相关文章

  1. centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课

    centos  shell脚本编程2 if 判断  case判断   shell脚本中的循环  for   while   shell中的函数  break  continue  test 命令   ...

  2. shell脚本中select循环语句用法

    shell脚本中select循环语句 1. 脚本中select的语法格式 select VAR in LIST do command1 command2 ... ... commandN done s ...

  3. shell脚本之for循环

    shell脚本之for循环 author :headsen  chen       2017-10-18    09:50:41 个人原创,转载请注明.否则依法追究法律责任 1,cat forloop ...

  4. shell 脚本中所有循环语法

    写出 shell 脚本中所有循环语法 for 循环 : for i in $(ls);do echo item:$i done while 循环 : #!/bin/bash COUNTER=0 whi ...

  5. shell脚本进阶之循环判断

    p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; f ...

  6. shell脚本程序中循环、判断语句的介绍

    shell的循环主要有3种,for,while,until shell的分支判断主要有2种,if,case 一,for循环 C/C++ Code复制内容到剪贴板 #!/bin/bash for fil ...

  7. Shell脚本之for循环、while循环,if语句、case语句

    1. for循环一般格式: 格式1: for((条件)) do 动作 done 格式2: for 变量名 in 范围 do 动作 done1234567891011121314实验:##1. 输出数字 ...

  8. shell 脚本中for循环

    昨天很痛苦的搞了一天的for循环,在服务器上运行没啥问题,在设备上运行总是不行,部分代码如下: for(i=1;i<$cnt+1;i++)do echo "xxxx"  &g ...

  9. Shell脚本- 单条命令循环执行重复工作

    关于shell for循环具体详细说明可参考:http://wiki.jikexueyuan.com/project/linux-command/chap34.html example: 分别在com ...

  10. [转]Shell脚本之无限循环的两种方法

    方法一: while循环,用的比较多的 #!/bin/bash set j= while true do let "j=j+1" echo "----------j is ...

随机推荐

  1. Python 求最大公因式~辗转相除法

    从错误中学python(4)——最小公约数与辗转相除法 网上看到一篇很简洁辗转相除法的写法:不用判断a,b的大小 def gcp(a, b): while(b%a!=0): a,b=b%a,a ret ...

  2. centos6.6部署mysql mmm高可用架构

    一.环境简述 1.工作逻辑图 2.MySQL-MMM优缺点 优点:高可用性,扩展性好,出现故障自动切换,对于主主同步,在同一时间只提供一台数据库写操作,保证的数据的一致性. 缺点:Monitor节点是 ...

  3. poj 1151(离散化+矩形面积并)

    题目链接:http://poj.org/problem?id=1151 关于离散化,这篇博客讲的很好:http://www.cppblog.com/MiYu/archive/2010/10/15/12 ...

  4. 关于yii2 的db log 日志 错误处理errorHandler

    log 通过配置Web.config来完成 1 数据库增加 ‘前缀_log’表 2 配置Web.config 'bootstrap' => ['log'], 'components' => ...

  5. J.U.C并发框架源码阅读(二)AbstractQueuedSynchronizer

    基于版本jdk1.7.0_80 java.util.concurrent.locks.AbstractQueuedSynchronizer 代码如下 /* * ORACLE PROPRIETARY/C ...

  6. POJ 2528.Mayor's posters-线段树(成段替换、离散数据、简单hash)

    POJ2528.Mayor's posters 这道题真的是线段数的经典的题目,因为数据很大,直接建树的话肯定不可以,所以需要将数据处理一下,没有接触离散化的时候感觉离散化这个东西相当高级,其实在不知 ...

  7. python 将windows字体中的汉字生成图片的方法

    #encoding: utf-8import osimport pygame chinese_dir = '黑体常规'if not os.path.exists(chinese_dir): os.mk ...

  8. (1)IIS

    1.选择控制面板-程序和功能 2.点击左侧“打开或关闭Windows功能 3.internet信息服务下的文件夹全勾 4.安装完成后,选择管理工具 5.选择IIS管理器 6.显示如下

  9. Codeforces Round #446 (Div. 2) A. Greed【模拟】

    A. Greed time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  10. thymeleaf初步使用

    thymeleaf模板引擎初步使用 #thymelea模板配置 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffi ...