当前位置:网站首页>shell脚本:for循环与while循环
shell脚本:for循环与while循环
2022-08-05 03:16:00 【UC 震惊部】
目录
一、for循环
for循环的简单案例:
计算1-100的偶数和
sum=0
for ((i=0 ;i<=100; i+=2)) //for i in { 0..100..2}
do
let sum=$i+$sum
done
echo "1-100的偶数和为: $sum"
例1:打印1-5这5个数字

例2:打印5次hello world
注意:虽然我们定义了一个变量i,但是没有使用它,它只是控制循环次数

例3:打印abcde

#!/bin/bash
for i in a b c d e
do
echo $i
done
例4:输出0-50之间的偶数
#!/bin/bash
for i in {0..50..2} //..2代表步长为2,每隔2个输出
do
echo $i
done
附1小技巧:花括号{}和seq在for循环的应用
- for i in {1..50..2] 1-50的奇数
- for i in {2..50..2} 1-50的偶数
- for i in { 10..1} 1-10倒序排列
- for i in $ (seq 10)1-10正序排列
- for i in $ (seq 10 -1 1)1-10倒序排列
- for i in $(seq 1 2 10)1-10的奇数,中间为步长
- for i in $ (seq 0 2 10) 1-10的偶数,中间为步长
例子
for i in $ (seq 0 2 10) ; do echo $i ; done
for循环的第一种格式
for 变量名
do
command
done
打印hello
#!/bin/bash
for i
do
echo hello
done 
第二种:
for i
do
echo $i
done
打印1-10的奇数
#!/bin/bash
for ((i=1;i<=10;i+=2)) //i=i+2
do
echo $i
done 
数字炸弹游戏: 要求在1-100内定义一个数字,与用户交互,要求,每次提醒用户,数字猜大了还是猜小了,直到猜中为止,最后统计猜的次数
PS:每次猜的数字不允许重复(脚本自身检测)
[[email protected] opt]# cat shu.sh
#!/bin/bash
sum=0 //次数
a=$(expr $[RANDOM%100+1])
for ((i=1;i<=20;i++))
do
read -p "请输入(0-100)一个整数: " nb
if [ $nb -eq $a ];
then
let sum++
echo "猜中,猜了$sum次"
exit //结束循环
elif [ $nb -lt $a ];
then
echo "猜小了"
let sum++
continue //退出本次循环,进行下一次
elif [ $nb -gt $a ];
then
echo "猜大了"
let sum++
continue
fi
done
二、while循环
while循环一般用于有条件判断的循环,若判断条件为真,则进入循环,当条件为假就跳出循环
1、语法结构
while 表达式
do
command
done
打印1-5
[rootserver ~]# vim while.sh
#!/bin/bash
i=1
while [ $i -le 5 ] (布尔值表达式)
do
echo $i
let i++ //注意这里如果不改变$i的值,会变成死循环
# i=$[$i+1] //两种写法
done
echo "最后i的值为: $i"
输出1-100之间不能被3整除的数字
[[email protected] myscripts]# vim 33.sh
#!/bin/bash
i=1
while [ $i -le 100 ]
do
if [[ $i%3 -ne 0 ]]
then
echo "$i"
fi
let i++
done
2、while死循环
while [ 1 -eq 1] //写一个永远为真的表达式,1等于1这个条件永远为真,所以这个脚本会一直循环下去
do
command
done
while true
do
command
done
while :
do
command
done
while truedo
let a++
echo $a
break (continue) exit
done
监控某服务(httpd)运行状态
#!/bin/bash
while ps aux | grep httpd | grep -v grep &> /dev/null
do
echo "httpd 正在运行中"
sleep 2
done
echo "httpd 不在运行"

猜商品价格游戏
$random用于生成0-32767的随机数
#!/bin/bash
PRICE=$(expr $RANDOM % 1000)
a=0
echo "商品实际价格范围为o-999,猜猜看是多少?"
while true
do
read -p "请输入你猜测的价格数目: " n
let a++
if [ $n -eq $PRICE ];
then
echo "恭喜你答对了,实际价格是$PRICE”
echo "你总共猜测了$a次"
exit 0
elif [ $n -gt $PRICE ];
then
echo "你猜高了!"
else
echo "你猜低了!"
fi
done 
案例:需求:首先要求有4家店,每家店 3个商品(3个商品的价格统一为100 200 300),然后要求以交互式的方式,让用户选择进入哪家店,并且选择哪个商品(用户如果进入商店,假设必定至少会买一件),最后统计此次消费价格
#!/bin/bash
i=1
sum=0
while [ $i -le 5 ]
do
echo "进入第$i家商店"
read -p "是否进入看看(yes/no)" doing
while [ $doing = "yes" ]
do
echo "1:衣服¥200"
echo "2:鞋子¥150"
echo "3:手套¥40"
echo "4:裤子¥155"
read -p "请选择需要购买的商品序列:" num
case $num in
1)
echo "衣服购买成功"
expr $[sum+=200] &> /dev/null
;;
2)
echo "鞋子购买成功"
expr $[sum+=150] &> /dev/null
;;
3)
echo "手套购买成功"
expr $[sum+=40] &> /dev/null
;;
*)
echo "裤子购买成功"
expr $[sum+=155] &> /dev/null
esac
read -p "是否继续进行购买(yes/no)" doing
done
let i++
if [ $doing = "no" ]
then
continue
fi
done
echo "购物总价:$sum"
3、循环控制语句
for循环一般会搭配条件判断语句和流程控制语句一起执行,那么就会出现需要跳过循环和中止循环的情况,控制循环的命令有以下3个
- continue 继续,但不会执行循环体内下面的代码了,开始重新开始下一次循环
- break 打断,马上停止本次循环,执行循环体外的代码
- exit 跳出循环
边栏推荐
- 2022-08-04 第六小组 瞒春 学习笔记
- private封装
- sql server 安装提示用户名不存在
- Intersection of Boolean Operations in SuperMap iDesktop.Net - Repairing Complex Models with Topological Errors
- 静态方法获取配置文件数据
- Is your data safe in this hyperconnected world?
- Object.defineProperty monitors data changes in real time and updates the page
- 调用阿里云oss和sms服务
- AI + Small Nucleic Acid Drugs | Eleven Completes $22 Million Seed Round Financing
- sql server installation prompts that the username does not exist
猜你喜欢

ASP.NET application--Hello World

倒计时 2 天|云原生 Meetup 广州站,等你来!

用CH341A烧录外挂Flash (W25Q16JV)

【已解决】Unity Coroutinue 协程未有效执行的问题

Matlab drawing 3
![[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)](/img/10/dafea90158adf9d43c4f025414fef7.png)
[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)

Talking about data security governance and privacy computing

毕设-基于SSM房屋租赁管理系统

2022-08-04 The sixth group, hidden from spring, study notes

基于生长的棋盘格角点检测方法
随机推荐
腾讯云【Hiflow】新时代自动化工具
【七夕节】浪漫七夕,代码传情。将爱意变成绚烂的立体场景,给她(他)一个惊喜!(送代码)
Is your data safe in this hyperconnected world?
从“能用”到“好用” 国产软件自主可控持续推进
One hundred - day plan -- -- DAY2 brush
语法基础(变量、输入输出、表达式与顺序语句完成情况)
2022.8.4-----leetcode.1403
ffmpeg -sources分析
leetcode - symmetric binary tree
Cloud Native (32) | Introduction to Platform Storage System in Kubernetes
Chinese characters to Pinyin
【已解决】Unity Coroutinue 协程未有效执行的问题
通过模拟Vite一起深入其工作原理
2022-08-04 The sixth group, hidden from spring, study notes
【 genius_platform software platform development 】 : seventy-six vs the preprocessor definitions written cow force!!!!!!!!!!(in the other groups conding personnel told so cow force configuration to can
Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals
MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
HDU 1114: Piggy-Bank ← The Complete Knapsack Problem
ASP.NET应用程序--Hello World
Linux下常见的开源数据库,你知道几个?