Shell中的while循环
来源:互联网 发布:c语言do while循环 编辑:程序博客网 时间:2024/05/16 08:21
while循环的格式
- while expression
- do
- command
- command
- ```
- done
1、计数器控制的while循环
主要用于已经准确知道要输入的数据和字符串的数目。
举例
- 1 #!/bin/sh
- 2 int=1
- 3 while(( $int<=5 ))
- 4 do
- 5 echo $int
- 6 let "int++"
- 7 done
2、结束标记控制的while循环
主要用于不知道读入数据的个数,但是可以设置一个特殊的数据值来结束循环,该特殊值称为结束标 记,通过提示用户输入进行操作。
举例
- 1 #用脚本演示使用结束标记控制while循环实现猜1~10内的数
- 2 #!/bin/sh
- 3
- 4 echo "Please input the num (1~~10): "
- 5 read num
- 6 while [[ $num != 4 ]]
- 7 do
- 8 if [ $num -lt 4 ]
- 9 then
- 10 echo "Too small ,Try again.."
- 11 read num
- 12 elif [ $num -gt 4 ]
- 13 then
- 14 echo "Too big ,Try again.. "
- 15 read num
- 16 else
- 17 exit 0
- 18 fi
- 19 done
- 20 echo "Yes ,you are right !!"
3、标致控制的while循环
用户输入标志值来控制循环结束
举例
- 1 #!/bin/sh
- 2 echo "Please input the num:"
- 3 read num
- 4 sum=0
- 5 i=1
- 6 signal=0
- 7 while [[ $signal != 1 ]]
- 8 do
- 9 if [ $i -eq $num ]
- 10 then
- 11 let "signal=1"
- 12 let "sum+=i"
- 13 echo "1+2、、、+$num=$sum"
- 14 else
- 15 let "sum=sum+i"
- 16 let "i++"
- 17 fi
- 18 done
4、命令行控制的while循环
举例
- 1 #!/bin/sh
- 2
- 3 echo "Please input arguements is $# "
- 4 echo "What you input : "
- 5 while [[ $* != "" ]]
- 6 do
- 7 echo $1
- 8 shift
- 9 done
0 0
- Shell中的while循环
- Shell中的while循环
- SHELL中的while循环
- shell 中的for循环和while循环
- Shell中的循环语句for、while、until
- shell 中的for循环while循环和case语句
- shell while循环监控
- shell脚本----while循环
- shell脚本--while循环
- shell while循环
- shell while循环问题
- 十七、Shell while循环
- Shell while循环
- Shell while循环
- Shell while循环
- Shell while循环
- shell while循环
- shell while循环
- oracle数据库学习
- JavaEE项目实战(OA系统)之八_部门管理之二
- ElasticSearch Tips
- Primer Plus 12章课后编程练习 5
- 谈谈UI架构设计的演化mvc-mvp-mvvm
- Shell中的while循环
- QTimer定时器的使用
- 什么情况下的网站不适宜转载内容?
- 4-Files and Directories
- iOS - 正则表达:NSRegularExpression的用法
- spark 2.0.0 开始了
- 复杂度体验-1
- 颠倒的价格牌
- 一致性Hash算法介绍(分布式环境算法)