转 http://blog.chinaunix.net/uid-25880122-id-2901409.html
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. 3
  4. 4 echo "Please input the num (1~~10): "
  5. 5 read num
  6. 6 while [[ $num != 4 ]]
  7. 7 do
  8. 8 if [ $num -lt 4 ]
  9. 9 then
  10. 10 echo "Too small ,Try again.."
  11. 11 read num
  12. 12 elif [ $num -gt 4 ]
  13. 13 then
  14. 14 echo "Too big ,Try again.. "
  15. 15 read num
  16. 16 else
  17. 17 exit 0
  18. 18 fi
  19. 19 done
  20. 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. 2
  3. 3 echo "Please input arguements is $# "
  4. 4 echo "What you input : "
  5. 5 while [[ $* != "" ]]
  6. 6 do
  7. 7 echo $1
  8. 8 shift
  9. 9 done
  10. 例子:
  11. echo "Please input the num:"
     4 read num
     5 #set -x
     6 while [ $num -gt 0 ]
     7 do
     8 >-------adb -s $1 shell am start -a android.intent.action.VIEW -d http://192
     9 >-------sleep 130
    10
    11 >-------let num=num-1
    12 >-------echo $num
    13 done
    14

shell 的while语句的更多相关文章

  1. (二)shell中case语句、程序传参、while

    2.2.6.1.case语句(1)shell中的case语句和C语言中的switch case语句作用一样,格式有差异(2)shell中的case语句天生没有break,也不需要break,和C语言中 ...

  2. shell编程——if语句【转载】

    (2)shell编程——if语句_macg_新浪博客http://blog.sina.com.cn/s/blog_6151984a0100ekl6.html shell编程——if语句转载 if 语句 ...

  3. shell的case语句简述(shell的流控制)

    shell流控制:http://www.cnblogs.com/yunjiaofeifei/archive/2012/06/12/2546208.html 1.if then else 语句 if t ...

  4. Shell 编程 until语句

    本篇主要写一些shell脚本until语句的使用. 计算1-50的和 #!/bin/bash i=0 s=0 until [ $i -eq 51 ];do let s+=i;let i++ done ...

  5. Shell 编程 循环语句

    本篇主要写一些shell脚本循环语句的使用. for 循环 指定次数 #!/bin/bash for ((i=1;i<=10;i++)) do echo $i done [root@localh ...

  6. Shell 编程 case语句

    本篇主要写一些shell脚本case语句的使用. 字符判断 #!/bin/bash read -p "请输入一个字符:" char case $char in [a-z]|[A-Z ...

  7. Shell 编程 条件语句

    本篇主要写一些shell脚本条件语句的使用. 条件测试 test 条件表达式 [ 条件表达式 ] 文件测试 -d:测试是否为目录(Directory). -e:测试文件或目录是否存在(Exist). ...

  8. Shell case in语句详解

    和其它编程语言类似,Shell 也支持两种分支结构(选择结构),分别是 if else 语句和 case in 语句.在<Shell if else>一节中我们讲解了 if else 语句 ...

  9. shell 的 功能语句--1

    [1]说明性语句 (1)shell 程序和语句 shell 程序由零或多条shell语句构成. shell语句包括三类:说明性语句.功能性语句和结构性语句. 说明性语句: 以#号开始到该行结束,不被解 ...

  10. shell条件测试语句实例-测试apache是否开启

    终于理解了shell条件测试语句"!="和"-n"的用法区别,于是有了如下的shell脚本,做为练习. 第一种方法:测试apache是否开启?字符串测试 #!/ ...

随机推荐

  1. .c和.h文件的区别

    .h文件(头文件): 一般写一些函数声明.宏定义.结构体等内容. 其实就是将各个.c文件中重复的声明.宏定义.结构体,枚举变量等提取出来,放进一个新的文件中,便于其他.c文件共享这部分的代码,同时也方 ...

  2. MySQL-5.7.21非图形化下载、安装、连接问题记录

    1.安装包下载链接:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-winx64.zip 官网:https://www.mysql.co ...

  3. excel 导入 下载模板 demo

    import org.apache.commons.beanutils.PropertyUtils;import org.apache.commons.lang3.StringUtils;import ...

  4. javascript中的浅拷贝和深拷贝(拷贝引用和拷贝实例)

    作者:千锋教育链接:https://www.zhihu.com/question/23031215/answer/326129003来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  5. 【java提高】(17)---Java 位运算符

    Java 位运算符 &.|.^.~.<<.>> 以前学过有关java的运算符,不过开发了这么久也很少用过这个.现在由于开发需要,所以现在再来回顾整理下有关java的运算 ...

  6. 配置Windows Server 2008环境

    上一章已经把Windows Server2008操作系统安装完毕,接下来配置一下Windows Server环境.配置网络和共享中心.配置桌面环境.配置用户IE设置.安装Telnet远程工具.配置文件 ...

  7. (三)c#Winform自定义控件-有图标的按钮

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  8. temperatureConversion1

    (原题:https://www.python123.io/student/courses/934/groups/8102/problems/programmings/6078) Solution: # ...

  9. 重读《学习JavaScript数据结构与算法-第三版》- 第4章 栈

    定场诗 金山竹影几千秋,云索高飞水自流: 万里长江飘玉带,一轮银月滚金球. 远自湖北三千里,近到江南十六州: 美景一时观不透,天缘有分画中游. 前言 本章是重读<学习JavaScript数据结构 ...

  10. react中babel的使用

    在开发中经常会使用到es6语法,那么如何能够很好兼容es6写法呢