当前位置:网站首页>A shell script the for loop statements, while statement
A shell script the for loop statements, while statement
2022-08-10 22:05:00 【Guannan cattle x people】
目录
How to turn a star into a triangle
随机生成密码(reach a certain level of complexity)
先定义一个变量while 表达式(条件)do Loop statement commanddone案例
Monitor memory and hard disk space in system resources
for循环语句
读取不同的变量值,用来逐个执行同一组命令
for循环经常使用在已经知道要进行多少次循环的场景
格式
for 变量名 in 取值列表
do
命令序列
done
案列
Display the printout1-7
案例2
案列3
小案列
小案列
This flashback also works,Including the curly brackets can be flexibly exchanged
without a list loop,We can specify parameters to print out
next case
If you want to see the operation process
批量创建用户,并设置密码
案例
根据ip地址列表检查主机状态
判断密码输入是否正确
Lucky member lucky draw
let :相当于 简单的计算器
**语 法**
let[计算表达式]
**功 能**
let 命令:是 BASH中用于计算的工具,Providing common operators also provides exponentiation“**”运算符.It does not need to be added in the house calculation of the variable$来表示变量,如果表达式的值是非0,Then the returned status value is 0;否则,The returned status value is 1
类C风格的for循环
for ((expr1;expr2;expr3))
do
command
doneexpr1:定义变量并赋初值
expr2:决定是否循环
expr3:决定循环变量如何改变,决定循环什么时候退出
显示输出1-5
#!/bin/bash
for((i=1;i<=5;i++)) #At this point you have to pay attention as long as there isi++ There are two parentheses
do
echo $i
done
嵌套 循环 星星
:
How to turn a star into a triangle
9 9乘法表
#!/usr/bin/env bash
for ((a=1;a<=9;a++))
do
for ((b=1;b<=9;b++))
do
if [[ a -ge b ]];then
echo -n "$b*$a=$[a*b] "
fi
done
echo " "
done
随机生成密码(reach a certain level of complexity)
#!/bin/bash
key="[email protected]#¥%%…………&&**(())————+"
num=${#key}
pass=''
for i in {1..6}
do
index=$[RANDOM%num]
pass=$pass${key:$index:1}
done
echo "When generating a randomly generated password:$pass"
法二:随机生成密码
while循环语句
while循环一般用于有条件判断的循环,若判断条件为真,则进入循环,当条件为假就跳出循环
先定义一个变量
while 表达式(条件)
do
Loop statement command
done
案例
打印1-5
1到100能被3整除的数字
#!/bin/bash
i=1
while [ $i -le 100 ]
do
if [[ $i%3 -ne 0 ]]
then
echo "$i"
fi
let i++
done
开启apache服务
#!/bin/bash
while ps aux |grep httpd |grep -v grep &>/dev/null
#ps aux Find all processes dynamically grep httpd :Filtering shows with httpd字段的 最>后 -v:取反 反向查找,过滤掉带有grep字段的
do
echo "Apache 服务正在运行中"
sleep 2 #睡眠两秒
done
echo "Apache Service is not running,Check to see if the service is down"
猜数字
whileThere is a way to solve the infinite loop,用break退出循环
#!/bin/bash
cnum=520
while true
do
read -p "请输入数字:" num
if [ $num -eq $cnum ];then
echo "你猜对了!"
break
elif [ $num -gt $cnum ];then
echo "Your guess is too big!"
elif [ $num -lt $cnum ];then
echo "Your guess is too small!"
fi
done
猜商品价格
#!/bin/bash
PC=`expr $RANDOM % 1000`
a=0
echo "The actual price range of the product0-999,Please guess how much?"
while true
do
read -p "请输入你猜的价格数目:" num
let a++
if [ $num -eq $PC ];then
echo "恭喜你答对了,实际价格是 $PC "
echo "你总共猜了$a次"
exit 0 #退出
elif [ $num -gt $PC ];then
echo "你猜高了!"
else
echo "你猜低了!"
fi
done
:
Monitor memory and hard disk space in system resources
#!/bin/bash
disk_size=$( df / |awk '/\//{print $4 }') #Free space on the hard disk
Mem_size=$(free | awk '/Mem/{print $4}') #The free space of the partition
while
do
if [ $disk_size -le 2560000 -a $Mem_size -le 102400 ];then
mail -s Warning root <<EOF
Insufficient memory and hard disk space, please deal with it as soon as possible!
EOF
#EOFis an interactive mode,Send things directly to the mailbox for warning#注意EOFYou must write at the top to succeed
fi
Done
商店购物
#!/bin/bash
i=1
sum=0
while [ $i -le 5 ]
do
echo "This princess loves No. 1 today$iwhat about the store"
read -p "是否进入看看(yes/no)" doing
while [ $doing = "yes" ]
do
echo "1:衣服 ¥200"
echo "2:鞋子 ¥150"
echo "3:帽子 ¥50"
echo "4:裤子 ¥165"
read -p "请选择需要购买的商品序列号:" num
case $num in
1)
echo "衣服购买成功!"
expr $[ sum+=200 ] &> /dev/null
;;
2)
echo "鞋子购买成功!"
expr $[ sum+=150 ] &> /dev/null
;;
3)
echo "帽子购买成功!"
expr $[ sum+=50 ] &> /dev/null
;;
*)
echo "裤子购买成功!"
expr $[ sum+=165 ] &> /dev/null
;;
esac
read -p "是否继续进行购买(yes/no)" doing
done
let i++
if [ $doing = "no" ]
then continue
fi
done
echo "Your total purchase today is :$sum"
if [ $sum -ge 150 ] && [ $sum -lt 200 ];then
let sum=sum-50
elif [ $sum -ge 200 ] && [ $sum -le 400 ];then
let sum=$sum-100
fi
echo "The price after discount is :$sum"
until 循环语句结构
重复测试某个条件,只要条件不成立则反复执行
until 条件测试操作
do
命令序列
done
用法: 重复测试某个条件,只要条件不成立则反复执行
只要while后面的命令退出状态为0,while循环就一直执行下去,until命令与while命令相似,唯一的区别在于,只要until后面的命令退出不为0,until循环就一直执行下去,也就是说,使用until语句重复执行一段代码直到条件为真为止,与while类似
计算1-50的和
#!/bin/bash
#功能1:0-50的和是多少?
i=0;s=0
until [ $i -eq 51 ]
do
let s+=i
let i++
done
echo $s
编写脚本的思路
1.First define the function of the script
案例:The requirement is to create a user and test the user20个
->It must use a loopfor、 while
->Know the meaning of the requirements to define the functionality of the script
2.Which commands are used when writing scripts?
as required above:就要使用useradd passwd let echo for while 等命令3.把变化的数据使用变量表示【Remember to define it first】
Fixed-length definition variable、Often called parameters can be set variable
Change the definition variable4.选择适合的流程控制
【循环语句、双分支、多分支、caseWait for some control statements 】
continue和break
break和continue都是用来控制循环结构的,主要是停止循环.
break 有时候我们想在某种条件出现的时候终止循环而不是等到循环条件为false才终止. 这时我们可以使用break来完成.break用于完全结束一个循环,跳出循环体执行循环后面的语句.
continue和break有点类似,区别在于continue只是终止本次循环,接着还执行后面的循环,break则完全终止循环. 可以理解为continue是跳过当次循环中剩下的语句,执行下一次循环.
使用方法
break:语法 break 将用于以下break语句退出循环
break n 这里n指定的第n个封闭的循环退出
continue:
语法 continue 和break语句一样,一个整数参数可以给continue命令跳过嵌套循环的命令
continue n 这里n指定第n个封闭循环 continue
边栏推荐
- 元宇宙社交应用,靠什么吸引用户「为爱发电」?
- 【PCBA solution】Electronic grip strength tester solution she'ji
- 化学制品制造业数智化供应链管理系统:建立端到端供应链采购一体化平台
- labelme-屏蔽拖拽的事件
- 2022.8.8好题选讲(数论场)
- Exploration and practice of the "zero trust" protection and data security governance system of the ransomware virus of Meichuang Technology
- Thread State 详解
- Bedtime story | made a Bitmap and AST length system configuration
- 【Maui正式版】创建可跨平台的Maui程序,以及有关依赖注入、MVVM双向绑定的实现和演示
- mmpose关键点(一):评价指标(PCK,OKS,mAP)
猜你喜欢
随机推荐
国内Gravatar头像的完美替代方案Cravatar
unusual understanding
黑猫带你学Makefile第12篇:常见Makefile问题汇总
华为路由器旁挂引流实验(使用流策略)
CGO Preliminary Cognition and Basic Data Type Conversion
管理员必须知道的RADIUS认证服务器的部署成本
Huawei router clock near the drainage experiment (using stream strategy)
黑猫带你学Makefile第11篇:当头文件a.h改变时,如何将所有依赖头文件a.h的.c文件都重新编译
[Maui official version] Create a cross-platform Maui program, as well as the implementation and demonstration of dependency injection and MVVM two-way binding
Live Classroom System 08-Tencent Cloud Object Storage and Course Classification Management
ArcMap创建镶嵌数据集、导入栅格图像并修改像元数值显示范围
PPT的两个实用技巧
ENVI自动生成地面控制点实现栅格影像的自动地理配准
接口测试的概念、目的、流程、测试方法有哪些?
C#【必备技能篇】Hex文件转bin文件的代码实现
边缘与云计算:哪种解决方案更适合您的连接设备?
谁是边缘计算服务的采购者?是这六个关键角色
Black cat takes you to learn Makefile Part 11: When the header file a.h changes, how to recompile all the .c files that depend on the header file a.h
【PCBA scheme design】Bluetooth skipping scheme
LeetCode-498 - Diagonal Traversal