当前位置:网站首页>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
边栏推荐
- Binary search tree (search binary tree) simulation implementation (there is a recursive version)
- The way of programmer architecture practice: how to design a sustainable evolution system architecture?
- [Output each bit of an integer, from high to low.With and without recursion]
- 本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现
- 代码分析Objective-C中的深拷贝与浅拷贝
- dataset数据集有哪些_数据集类型
- 【MySQL】数据库进阶之索引内容详解(上篇 索引分类与操作)
- C - 为什么指针常常初始化为 NULL?
- LP流动性挖矿DAPP系统开发丨流动性挖矿功能原理及说明
- Traceback (most recent call last): File
猜你喜欢
本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现
LeetCode 899 有序队列[字典序] HERODING的LeetCode之路
【MySQL功法】第5话 · SQL单表查询
Dry goods!A highly structured and sparse linear transformation called Deformable Butterfly (DeBut)
Polymorphism in detail (simple implementation to buy tickets system simulation, covering/weight definition, principle of polymorphism, virtual table)
基于SSM和Web实现的农作物生长监控系统
一个扛住 100 亿次请求的红包系统,写得太好了!!
Analysis of the idea of the complete knapsack problem
【MySQL】数据库进阶之索引内容详解(上篇 索引分类与操作)
码率vs.分辨率,哪一个更重要?
随机推荐
[论文阅读] (23)恶意代码作者溯源(去匿名化)经典论文阅读:二进制和源代码对比
FR9811S6 SOT-23-6 23V,2A同步降压DC/DC转换器
GET 和 POST 有什么区别?
【TypeScript】Why choose TypeScript?
本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现
LeetCode-1796. 字符串中第二大的数字
viewstub 的详细用法_pageinfo用法
【一起学Rust 基础篇】Rust基础——变量和数据类型
【一起学Rust】Rust的Hello Rust详细解析
永寿 永寿农特产品-苹果
浅谈SVN备份
opencv学习—VideoCapture 类基础知识「建议收藏」
[Bubble sort and odd-even sorting]
This article takes you to understand the principle of CDN technology
Realize 2d characters move left and right while jumping
MySQL之json数据操作
ERC20通证标准是什么?
赛灵思MPSOC裸机下的 USB调试实验
微信小程序获取用户手机号码
fast planner中拓扑路径搜索