当前位置:网站首页>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
边栏推荐
- Likou 215 questions, the Kth largest element in an array
- CGO Preliminary Cognition and Basic Data Type Conversion
- ACM模板笔记:八数码问题——使用BFS+康托展开打表解决
- Labelme-5.0.1 version edit polygon crash
- ACM模板笔记:最长不下降/上升子序列
- 阿里巴巴、蚂蚁集团推出分布式数据库 OceanBase 4.0,单机部署性能超 MySQL
- Extended Chinese Remainder Theorem
- 地理探测器Geodetector软件的下载、应用与结果解读
- ArcMap时间滑块功能动态显示图层数据并生成视频或动图
- 变量和它的特性——《mysql 从入门到内卷再到入土》
猜你喜欢
LeetCode-498-对角线遍历
Play RT-THREAD of doxygen
Using SylixOS virtual serial port, serial port free implementation system
着力提升制造业核心竞争力,仪器仪表产业迎高质量发展
企业云存储日常运行维护实践经验分享
C#【必备技能篇】Hex文件转bin文件的代码实现
C # Hex file transfer skills necessary article 】 【 bin file code implementation
ACM解题笔记——HDU 1401 Solitaire(DBFS)
【PCBA solution】Electronic grip strength tester solution she'ji
FPGA - Memory Resources of 7 Series FPGA Internal Structure -03- Built-in Error Correction Function
随机推荐
函数:函数删除操作语法&使用例——《mysql 从入门到内卷再到入土》
Live Classroom System 09--Tencent Cloud VOD Management Module (1)
华为路由器旁挂引流实验(使用流策略)
Using SylixOS virtual serial port, serial port free implementation system
Application of Spatial 3D Model Reconstruction Based on Pix4Dmapper - Spatial Analysis and Site Selection
GAN CFOP
shell(文本打印工具awk)
【PCBA方案设计】蓝牙跳绳方案
MATLAB神经网络拟合工具箱Neural Net Fitting使用方法
Shell编程之条件语句(二)
xshell (sed 命令)
3D model reconstruction of UAV images based on motion structure restoration method based on Pix4Dmapper
国内Gravatar头像的完美替代方案Cravatar
12 Recurrent Neural Network RNN2 of Deep Learning
SELECT:简单的查询操作语法&使用例——《mysql 从入门到内卷再到入土》
交换机和生成树知识点
ArcGIS自动随机生成采样点的方法
Shell编程规范与变量
Object.assign用法 以及 与$.extend的区别
服务——DNS正向反向域名解析服务