ITPub博客

首页 > > > Shell之while循环

Shell之while循环

作者:longmanma 时间:2019-12-17 18:07:43 0 删除 编辑

While循环的格式:

while expressiondocommand

command

、、、done

1、计数器控制的while循环:

主要用于已经准确知道要输入的数据和字符串的数目。

例子:

#!/bin/bashint=1while(($int<=5 ))doecho$intlet "int++"done

2、结束标记控制的while循环

主要用于不知道读入数据的个数,但是可以设置一个特殊的数据值来结束循环,该特殊值称为结束标记,通过提示用户输入进行操作。

例子:

#!/bin/bash

#用脚本演示使用结束标记控制while循环实现猜1~10内的数echo"Please input the num (1~~10): "read numwhile[[ $num !=4 ]]doif[ $num -lt4];thenecho"Too small,Try again.." read numelif[ $num -gt4];thenecho"Too big,Try again.." read numelse exit 0fidoneecho"Yes,you are right !!"

3、标志控制的while循环

用户输入标志值来控制循环的结束

例子:

#!/bin/bashecho"Please input the num: "read numsum=0i=1signal=0#while[[ $signal !=1 ]]while(($signal !=1))doif[ $i -eq $num ];then let "signal=1" let "sum+=i"echo"1+2、、、+$num=$sum"else let "sum=sum+i" let "i++"fidone

4、命令行控制的while循环

例子:

#!/bin/bashecho"Please iput arguements is $# "echo"What you input : "while[[ $* !="" ]]doecho$1shiftdone

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2730/viewspace-3131061/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: Now that we are tr
请登录后发表评论 登录
全部评论

注册时间:2019-12-18

  • 博文量
    492
  • 访问量
    11988