Shell中的while循环
来源:互联网 发布:改变照片底色的软件 编辑:程序博客网 时间:2024/05/22 13:36
http://blog.chinaunix.net/uid-25880122-id-2901409.html
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循环
- GeoServer发布Heatmap
- 《深入理解java虚拟机》Java内存结构及内存溢出实例
- 检查邮箱是否为正确的格式
- Android ListView使用BaseAdapter与ListView的优化
- java网络爬虫简单实现
- Shell中的while循环
- 超级电容【ultra capicator】是什么?
- iOS之Core Bluetooth for iOS
- win7计算器功能详解
- postgres中的用户与模式
- java反射详解
- Android fill_parent、wrap_content和match_parent的区别
- linux颜色来表示文件类型:
- oop 获取当天 凌晨