当前位置:网站首页>bash while循环和until循环
bash while循环和until循环
2022-08-03 11:34:00 【小青头】
bash while循环和until循环
在bash中,我们除了可以使用for循环,也可以使用while循环和until循环去实现我们需要的功能;这里我总结了while和until循环相关的用法。
while循环
while循环格式如下
while 测试条件;do
语句1
语句2
...
done
示例:计算100以内所有正整数的和
[[email protected] bash_test]# cat while.sh
#!/bin/bash
Sum=0
Count=1
while [ $Count -le 100 ];do
let Sum+=$Count
let ++Count
done
echo "sum of 1 to 100 : $Sum"
[[email protected] bash_test]# ./while.sh
sum of 1 to 100 : 5050
注意:
- while循环体可以用命令;[];[[]] 三种形式
括号内的表达式要有空格[空格$Count -le 100空格]
表达式判断大小应该使用:小于-lt ,大于gt, 等于eq, 小于等于le, 大于等于ge, 不等于ne - 自增可以使用
let ++Count,同时也可以使用Count=$[$Count+1]
同时let Sum+=$Count可以替换为Sum=$[$Sum+$Count] - let可以限时表达式做算数运算,而不是字符运算
until循环
until循环格式如下
until 测试条件; do
语句1
语句2
....
done
示例:计算100以内所有偶数的和
[[email protected] bash_test]# cat EvenSum.sh
#!/bin/bash
Sum=0
Count=0
until [ $Count -gt 100 ];do
Count=$[$Count+2]
Sum=$[$Sum+$Count]
done
echo "Sum of all even number from 0 to 100 :$Sum"
[[email protected] bash_test]# ./EvenSum.sh
Sum of all even number from 0 to 100 :2652
总结
- while和until循环都是用于在事先不知道循环次数的情况下使用,for循环经常用于已知循环次数的情况下
- while循环:当条件满足时,执行循环;不满足条件时,退出循环
- until循环:当不满足条件时,执行循环;满足条件,就退出循环
更多示例
- 如果用户的ID号为偶数时,显示其名称和shell;对所有用户执行此操作
[[email protected] bash_test]# cat showEvenUser.sh
#!/bin/bash
while read Line;do
UserId=`echo $Line | cut -d: -f3`
if [ $[$UserId%2] -eq 0 ];then
echo $Line | cut -d: -f1,7
fi
done < /etc/passwd
[[email protected] bash_test]# ./showEvenUser.sh
root:/bin/bash
daemon:/sbin/nologin
注意:while 循环可以接受文件路径,读取文件中每一行
while read Line;do
语句1
语句2
...
done < /path/to/somefile
2.转换用户输入的字符为大写,除了quit(遇见quit退出)
[[email protected] bash_test]# cat toUpper.sh
#!/bin/bash
read -p "input you string(quit is quitting):" content
until [ "$content" == "quit" ];do
echo $content | tr 'a-z' 'A-Z'
read -p "input you string(quit is quitting):" content
done
[[email protected] bash_test]# ./toUpper.sh
input you string(quit is quitting):apple
APPLE
input you string(quit is quitting):hello world
HELLO WORLD
input you string(quit is quitting):quit
注意:字符串变量在做比较时,最好包裹在双引号中
3.每隔5秒查看hadoop用户是否登录,如果登录,则显示登录并退出;否则,显示当前时间,并说明hadoop尚未登录
[[email protected] bash_test]# cat ./checkUser.sh
#!/bin/bash
until who | grep "^hadoop" &> /dev/null;do
date
sleep 5
echo "hadoop not login"
done
echo "hadoop is here"
[[email protected] bash_test]# ./checkUser.sh
2022年 07月 29日 星期五 20:59:36 CST
hadoop not login
2022年 07月 29日 星期五 20:59:41 CST
hadoop not login
hadoop is here
边栏推荐
猜你喜欢

Matlab学习10-图像处理之傅里叶变换

Redis发布订阅和数据类型

【LeetCode—第2题 两数之和 代码详解 】附有源码,可直接复制

html网页如何获取后台数据库的数据(html + ajax + php + mysql)

Babbitt | Metaverse daily must-read: Players leave, platforms are shut down, and the digital collection market is gradually cooling down. Where is the future of the industry?...
![[Detailed explanation of binary search plus recursive writing method] with all the code](/img/51/c4960575a59f8ca7f161b310e47b27.png)
[Detailed explanation of binary search plus recursive writing method] with all the code

字节最爱问的智力题,你会几道?

asdn涨薪技术之apifox+Jenkins如何玩转接口自动化测试

GET 和 POST 有什么区别?

后台图库上传功能
随机推荐
RTP协议分析
【TypeScript】Why choose TypeScript?
矩阵的计算[通俗易懂]
直播弱网优化
【MySQL功法】第5话 · SQL单表查询
【一起学Rust】Rust的Hello Rust详细解析
LeetCode 899 Ordered queue [lexicographical order] HERODING's LeetCode road
Fastjson反序列化
【一起学Rust】Rust学习前准备——注释和格式化输出
hystrix 服务熔断和服务降级
赛灵思MPSOC裸机下的 USB调试实验
VRRP协议的作用及VRRP+OSPF配置方法
Traceback (most recent call last): File
数据库一席谈:打造开源的数据生态,支撑产业数字化浪潮
什么是bin文件?「建议收藏」
opencv学习—VideoCapture 类基础知识「建议收藏」
Matlab学习11-图像处理之图像变换
[Detailed explanation of binary search plus recursive writing method] with all the code
【二分查找详解外加递归写法】附有全部代码
QGIS绘制演习区域示意图