当前位置:网站首页>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
边栏推荐
- 使用SylixOS虚拟串口,实现系统串口自由
- 异常的了解
- ENVI最小距离、最大似然、支持向量机遥感影像分类
- APP UI自动化测试常见面试题,或许有用呢~
- MySQL高级指令
- QT笔记——vs + qt 创建一个带界面的 dll 和 调用带界面的dll
- [Maui official version] Create a cross-platform Maui program, as well as the implementation and demonstration of dependency injection and MVVM two-way binding
- INSERT:插入操作语法&使用例——《mysql 从入门到内卷再到入土》
- 深度学习之 12 循环神经网络RNN2
- Extended Chinese Remainder Theorem
猜你喜欢

华为路由器旁挂引流实验(使用流策略)

12 Recurrent Neural Network RNN2 of Deep Learning

PROCEDURE :存储过程结构——《mysql 从入门到内卷再到入土》

FPGA - 7系列 FPGA内部结构之Memory Resources -03- 内置纠错功能

xshell (sed command)

元宇宙社交应用,靠什么吸引用户「为爱发电」?

爬虫request.get()出现错误

Redis 性能影响 - 异步机制和响应延迟

Play RT-THREAD of doxygen

LeetCode-36-Binary search tree and doubly linked list
随机推荐
流程控制结构——《mysql 从入门到内卷再到入土》
ArcMap创建镶嵌数据集、导入栅格图像并修改像元数值显示范围
CGO Preliminary Cognition and Basic Data Type Conversion
使用SylixOS虚拟串口,实现系统串口自由
uni-app微信小程序——下拉多选框
Application of Spatial 3D Model Reconstruction Based on Pix4Dmapper - Spatial Analysis and Site Selection
UPDATE:修改数据语法使用例——《mysql 从入门到内卷再到入土》
《mysql 从入门到内卷再到入土》——Mysql基础 学习笔记目录
扩展中国剩余定理
Service - DNS forward and reverse domain name resolution service
这些不可不知的JVM知识,我都用思维导图整理好了
Object.assign用法 以及 与$.extend的区别
【开源教程5】疯壳·开源编队无人机-飞控固件烧写
关于 DataFrame: 处理时间
C # Hex file transfer skills necessary article 】 【 bin file code implementation
labelme-屏蔽拖拽的事件
labelme-5.0.1版本编辑多边形闪退
基于Pix4Dmapper的运动结构恢复法无人机影像三维模型重建
自建函数 测试例和语法——《mysql 从入门到内卷再到入土》
Labelme-5.0.1 version edit polygon crash