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循环

    while循环的格式   while expression do command command ``` done 1.计数器控制的while循环    主要用于已经准确知道要输入的数据和字符串的数目 ...

  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. 【java并发编程实战】第五章:基础构建模块

    1.同步容器类 它们是线程安全的 1.1 vector和hashtable. 和Collections.synchronizeXxx()一样.实现方式就是在每个方法里面加入synchronize代码块 ...

  2. wpf显示视频,image控件闪屏,使用winform控件实现

    使用C#调用mingw的动态库实现视频识别软件,程序通过C++调用opencv打开视频,将图像的原始数据以rgb24的方式传递给C#端,C#通过构造图像对象给控件赋值的方式显示图片. 一开始使用wpf ...

  3. python 面试题: 列表表达式

    [process() for item1 in iterable1 if condition1 for item2 in iterable2 if condition2 For item3 in it ...

  4. gdb coredum 信息例子

    http://blog.csdn.net/hzhsan/article/details/38227647

  5. 持久化ORM框架——Hibernate与mybatis

    最初SUN公司推出了JavaEE服务器端组件模型(EJB),但是由于EJB配置复杂,且适用范围较小,于是很快就被淘汰了.与EJB的失败伴随而来的是另外一个框架的应运而生.他就是至今也比较流行的Hibe ...

  6. OpenCV漫水填充算法示例代码

    #include<cv.h> #include<highgui.h> int main(int argc, char** argv) { IplImage* img = cvL ...

  7. [剑指Offer] 23.二叉搜索树的后序遍历

    [思路]BST的后序序列的合法序列是,对于一个序列S,最后一个元素是x (也就是根),如果去掉最后一个元素的序列为T,那么T满足:T可以分成两段,前一段(左子树)小于x,后一段(右子树)大于x,且这两 ...

  8. input属性 disabled与readonly的区别

    从效果上看 源码 <!DOCTYPE html> <html> <head lang="en"> <meta charset=" ...

  9. Codeforce 721C DP+DAG拓扑序

    题意 在一个DAG上,从顶点1走到顶点n,路径上需要消费时间,求在限定时间内从1到n经过城市最多的一条路径 我的做法和题解差不多,不过最近可能看primer看多了,写得比较复杂和结构化 自己做了一些小 ...

  10. 【CF edu 30 A. Chores】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...