当前位置:网站首页>Shell loop statement (for, while, until)
Shell loop statement (for, while, until)
2022-08-04 11:44:00 【Mans to malicious】
目录
一、循环语句
1、for循环基础
在实际工作中,经常会遇到某项任务需要多次执行的情况,而每次执行时仅仅是处理的对象不一样,其他命令相同.
forThe statement structure of the loop statement
for 变量名 in $LIST (1、The element to be assigned to the variable 2、决定循环次数)
do
命令系列
done或者
for ((i=*;i<=*;1++)) (Define the variable start value;Defines the loop end condition;控制循环次数)
do
命令序列
done
for语句的执行流程
First the variable will take the first value in the value list,Then go to execute the command series,执行完成后,Then go to get the second value of the list of values,Then go to execute the command sequence,It is not executed until all the values in the value list have been fetcheddone,跳出循环.

Basic tips 大括号 {} 和 seq 在for循环的应用
for i in {1..5..2} 1-50的奇数
for i in {2..6..2} 1-50的偶数
for i in {3..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的偶数,中间为步长


2、for案例
①Accumulates the input integers

② Perform odd and even judgment on the input number

③Odd sum of the input numbers、偶数求和

④Create users in bulk from a list

Delete users in bulk based on the list

⑤密码验证

⑥9*9乘法表


⑦guess the bomb game

⑧三角形


3、while 语句
1、while 语句说明
for循环语句非常适用于列表对象无规律,且列表来源已固定(如某个列表文件)的场合.而对于要求控制循环次数,操作对象按数字顺序编号、按特定条件执行重复操作等情况,It is more suitable to apply another cycle-----while语句
while 条件测试操作 (布尔值)
do
命令序列
done

2、while案例
vim 14.sh
#!/bin/bash
i=1 #定义变量i=1
while [ $i -le 10 ] #重复测试$1是否小于等于10,直至$i等于10
do #命令列表
if [[ $i%3 -ge 0 ]] #条件检测 $i取余3,是否等于0
then #条件成立
echo "$i" #输出 $i的值
fi #结束判断
let i++ #每次循环i+1
done #结束循环

② 猜数字


③ 商场购物
Let users choose whether to enter the store to shop,You can only enter three shops at most,Products and prices are displayed in each store,The user chooses to make a purchase,Finally, the prices of all the products purchased by the user are accumulated


3、until语句
跟while相反,条件为假进入循环,条件为真退出循环
#!/bin/bash
i=0 //定义变量i=0
j=0
until [ $i -eq 11 ] //$i等于11时停止执行
do
let j=j+i // 或者 let j+=i
let i++ //每次循环i+1
done
echo "$j //打印结果 $j

二、循环控制语句
for循环一般会搭配条件判断语句和流程控制语句一起执行,那么就会出现需要跳过循环和中止循环的情况,控制循环的命令有以下3个
1、continue
①打印1-5, 3不打印

2、break *
打断,马上停止本次循环,执行循环体外的代码

3、exit
直接跳出程序,后面可跟状态返回码,如:exit 100

总结
循环语句有 for、while 和 untileThree unformatted loop statements,The first two have the same effect,There are differences in usage,until使用较少,Contrary to the previous statement,注意break和continue和exit的使用方式.
break的使用方式: 跳出当前的循环,
continue的使用方式: 继续当前循环,There will be no output after this loop,The cycle continues to start,
exit的使用方式: 退出当前终端,when executing the script,Try to use path execution,如果使用source执行的话,The internal variables will be loaded into the system environment for execution,The terminal program will then be exited.
边栏推荐
- MTBF是什么意思?交换机做MTBF有什么要求?MTTF、MTBF和MTTR的区别是什么?
- 十一、网络规划设计
- Leetcode brush questions - 543. Diameter of binary trees, 617. Merging binary trees (recursive solution)
- 【LeetCode】98.验证二叉搜索树
- IBM Q复制启动停止查看状态
- 动手学深度学习_LeNet
- 『快速入门electron』之实现窗口拖拽
- 将博客搬至CSDN
- POJ1094Sorting It All Out题解
- 【目标检测】yolov2特征提取网络------Darknet19结构解析及tensorflow和pytorch实现
猜你喜欢
随机推荐
数据库对象-视图;存储过程
技术干货 | 用零信任保护代码安全
Leetcode - using sequence traversal features first completed 114. The binary tree to the list
200PLC转以太网与研华webaccess modbusTCP客户端在空调机上应用配置案例
Xilinx VIVADO 中 DDR3(Naive)的使用(2)读写设计
Mysql数据类型
Based on the BiLSTM regression forecast method
涨姿势了!原来这才是多线程正确实现方式
244页pdf!《2022 中国云计算生态蓝皮书》发布
TPC藏宝计划IDO自由协议复利模式开发功能分析
光盘刻录步骤
职责链模式(responsibilitychain)
监督和半监督学习下的多标签分类综述
ESP8266-Arduino编程实例-APDS-9930环境光和趋近感器驱动
ECCV 2022 | 通往数据高效的Transformer目标检测器
WPF 截图控件之画笔(八)「仿微信」
多行函数;group_by分组;having分组后筛选;单表查询总结
十一、网络规划设计
*W3C* Standards Organization
项目管理前景









