while循环的格式
 
  1. while expression
  2. do
  3. command
  4. command
  5. ```
  6. done
1、计数器控制的while循环
   主要用于已经准确知道要输入的数据和字符串的数目。
   举例
  1. 1 #!/bin/sh
  2. 2 int=1
  3. 3 while(( $int<=5 ))
  4. 4 do
  5. 5 echo $int
  6. 6 let "int++"
  7. 7 done
2、结束标记控制的while循环
      主要用于不知道读入数据的个数,但是可以设置一个特殊的数据值来结束循环,该特殊值称为结束标    记,通过提示用户输入进行操作。
举例
  1. 1 #用脚本演示使用结束标记控制while循环实现猜1~10内的数
  2. 2 #!/bin/sh
  3. 4 echo "Please input the num (1~~10): "
  4. 5 read num
  5. 6 while [[ $num != 4 ]]
  6. 7 do
  7. 8 if [ $num -lt 4 ]
  8. 9 then
  9. 10 echo "Too small ,Try again.."
  10. 11 read num
  11. 12 elif [ $num -gt 4 ]
  12. 13 then
  13. 14 echo "Too big ,Try again.. "
  14. 15 read num
  15. 16 else
  16. 17 exit 0
  17. 18 fi
  18. 19 done
  19. 20 echo "Yes ,you are right !!"
3、标致控制的while循环
   用户输入标志值来控制循环结束
 举例
 
  1. 1 #!/bin/sh
  2. 2 echo "Please input the num:"
  3. 3 read num
  4. 4 sum=0
  5. 5 i=1
  6. 6 signal=0
  7. 7 while [[ $signal != 1 ]]
  8. 8 do
  9. 9 if [ $i -eq $num ]
  10. 10 then
  11. 11 let "signal=1"
  12. 12 let "sum+=i"
  13. 13 echo "1+2、、、+$num=$sum"
  14. 14 else
  15. 15 let "sum=sum+i"
  16. 16 let "i++"
  17. 17 fi
  18. 18 done
4、命令行控制的while循环
  举例
  1. 1 #!/bin/sh
  2. 3 echo "Please input arguements is $# "
  3. 4 echo "What you input : "
  4. 5 while [[ $* != "" ]]
  5. 6 do
  6. 7 echo $1
  7. 8 shift
  8. 9 done

Shell中的while循环的更多相关文章

  1. shell编程学习笔记(十):Shell中的for循环

    shell编程中可以实现for循环遍历 先来写一个最简单的吧,循环输出从1到10,脚本内容为: #! /bin/sh for i in {1..10} do echo $i done 上面的代码从1到 ...

  2. shell中的for循环用法详解

    for i in “file1” “file2” “file3”for i in /boot/*for i in /etc/*.conffor i in $(seq -w 10) –>等宽的01 ...

  3. shell 中可以for 循环的时间加减日期格式

    ..};do # LAST_HOUR=`date -d '-${num} hour' +%H` 不可for循环,报格式错误 LAST_HOUR=`date "+%H" -d -${ ...

  4. Shell中的while循环【转】

    转自:http://blog.chinaunix.net/uid-25880122-id-2901409.html while循环的格式   while expression do command c ...

  5. shell 中的for循环

    第一类:数字性循环 #!/bin/bash for((i=1;i<=10;i++)); do echo $(expr $i \* 3 + 1); done #!/bin/bash for i i ...

  6. shell中for循环总结

    关于shell中的for循环用法很多,一直想总结一下,今天网上看到上一篇关于for循环用法的总结,感觉很全面,所以就转过来研究研究,嘿嘿... 1. for((i=1;i<=10;i++));d ...

  7. 02: shell中的if、case、for等语句

    目录: 1.1 shell中常用运算符 1.2 使用if条件语句 1.3 shell 中的for循环 1.4 shell中的while循环语句 1.5 使用case分支语句 1.1 shell中常用运 ...

  8. MongoDB 学习笔记(二):shell中执行增删查改

    一.查 1.查询集合中所有文档:db.集合名.find(). 2.查询集合中第一个文档:db.集合名.findOne(). 3.指定查询条件:第一个参数就是指定查询条件 查询全部文档:db.集合名.f ...

  9. shell中的循环

    shell中的循环 for循环 类似于C语言的步长控制 例如: ;i<=;i++)); ); done 将1到10,依次乘以4,然后打印出来. 这里顺便提一下,shell里面表达式的计算,可以有 ...

随机推荐

  1. 内容提供器(ContentProvider)

    一.简介内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访数据的安全性.目前,使 ...

  2. js 参数校验器

    //校验器 var validate = { //校验当前运行环境是否是手机端 isWap:function(){ var sUserAgent= navigator.userAgent.toLowe ...

  3. Oracle 字符串不为空条件

    Oracle 中,空字符串存入到Oracle中会自动转换为NULL,另外VARCHAR2把空串等同于null处理. SQL from dual where null=null; 没有查到记录 SQL ...

  4. Eclipse中设置格式化jsp自动换行

    JSP代码换行:Window->Preferences->Web->JSP Files->Editor->Line width

  5. 测试代码覆盖率工具学习(Android Emma)

    博客分类: 工具分享 eclipseeclemmaemmatestng       关于eclemma的历史和怎么安装,请参考http://www.ibm.com/developerworks/cn/ ...

  6. 解决zabbix“ZBX_NOTSUPPORTED: Timeout while executing a shell script”报错

    如题所示,在zabbix_server使用zabbix_get获取自定义“UserParameter”对应的脚本的数据时,出现了如题所示的报错信息 [root@nmp01 scripts]# /usr ...

  7. <转>Windows平台下Makefile学习笔记(二)

    本文转自:http://blog.csdn.net/clever101/article/details/8286066 上次我们学习了怎么用Makefile编译一个控制台工程.这次我们学习一下如何使用 ...

  8. Linux下, Eclipse C/C++ IDE下编辑好C/C++源程序之后要先保存!!!否则,就会……

    注意:Linux下, Eclipse C/C++ IDE下编辑好C/C++源程序之后要先保存! ! ! 然后Project-->Build All/Build Project,再点绿箭头执行.否 ...

  9. 基于CentOS7的服务器搭建(LAMP环境)

    基于CentOS7的服务器环境搭建(LAMP环境) 一.安装MySQL组件 1.由于在CentOS7中,默认yum安装库中不含有mysql,我们可以下载mysql的分支MariaDB,如果必须要下my ...

  10. Google 全球 IP 地址库

    ## Google 全球 IP 地址库 IP 地址来源:http://www.kookle.co.nr Bulgaria 93.123.23.1 93.123.23.2 93.123.23.3 93. ...