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. Laravel之命令

    一.创建命令 php artisan make:console SendEmails 上述命令将会生成一个类app/Console/Commands/SendEmails.php,当创建命令时,--c ...

  2. 【LeetCode】Sort Colors 数组排序

    题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...

  3. object-c 框架之经常使用结构体

    Foundation 框架定义经常使用结构体.结构体採用object-c 定义:经常使用NSSRange,NSPoint.NSSize,NSRect等 一.NSRange 创建范围结构体. 方法:NS ...

  4. shell 命令行语句

    第一步:ssh免密码登陆[用公钥,私钥] 第二步: #!/bin/bash while read server;do ssh -n $server >& |sed "s/^/$ ...

  5. Centos7 搭建最新 Nexus3 Maven 私服

    Maven 介绍 Apache Maven 是一个创新的软件项目管理和综合工具.Maven 提供了一个基于项目对象模型(POM)文件的新概念来管理项目的构建,可以从一个中心资料片管理项目构建,报告和文 ...

  6. 编写批处理命令来执行JAVA文件

    http://baobeituping.iteye.com/blog/802543 1. JAVA工程路径结构 Project    ------lib    ------src    ------W ...

  7. C# 写日志到文件

    C# 写日志到文件 using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms ...

  8. CentOS4.5下LVS方案

    环境描述:本文在配置LVS时使用三台linux,一台做Directorserver (192.168.0.25) ,两台做realserver(192.168.0.127 192.168.0.12,在 ...

  9. background API

    语法: background:bg-color bg-image position/bg-size bg-repeat bg-origin bg-clip bg-attachment initial| ...

  10. js 正则表达式 取反

    http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp 以匹配中文为例 const test_value = '李钊鸿' if (/[^\u4e00 ...