当前位置:网站首页>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.
边栏推荐
- 动手学深度学习_LeNet
- 监督和半监督学习下的多标签分类综述
- [Flight Control Development Advanced Course 7] Crazy Shell Open Source Formation UAV - Formation Flight
- 终于有人把分布式机器学习讲明白了
- Move the blog to CSDN
- 上帝空间——全球首个基于Web3.0的艺术协议创意平台,拓宽多元艺术融合边界
- 面试蚂蚁(P7)竟被MySQL难倒,奋发图强后二次面试入职蚂蚁金服
- mysqldump远程备份数据库
- Leetcode Brush Questions - Path Sum
- 复盘:经典的HR面试问题,这些问题可以挖掘你个人的素质,看看你是否合适合我们部门
猜你喜欢

【目标检测】YOLOv4特征提取网络——CSPDarkNet53结构解析及PyTorch实现

ECCV 2022 | Towards Data Efficient Transformer Object Detectors

Transferring Rich Feature Hierarchies for Robust

光盘刻录步骤

MySQL索引原理以及SQL优化

【全网首发】Redis系列5:深入分析Cluster 集群模式

ESP8266-Arduino编程实例-MQ3酒精传感器驱动

中电金信技术实践|分布式事务简说

深度学习------pytorch-gpu环境搭建

【目标检测】yolov3特征提取网络------Darknet53网络及pytorch实现
随机推荐
Leetcode - using sequence traversal features first completed 114. The binary tree to the list
企业应当实施的5个云安全管理策略
Tapdata 开源项目基础教程:功能特性及实操演示
【目标检测】YOLOv4特征提取网络——CSPDarkNet53结构解析及PyTorch实现
The use of DDR3 (Naive) in Xilinx VIVADO (1) to create an IP core
【黄啊码】MySQL入门—2、使用数据定义语言(DDL)操作数据库
200ppi转以太网通过4Gwifi在医药设备移动平台(平板电脑、手机)
*SEO*
临床研究方法学,到现场,到数据真实发生的地方 | 对话数智 x 张维拓
Implementation principle of function emplace_back in vector
【黄啊码】MySQL入门—1、SQL 的执行流程
DDL和DML的补充
Rust 从入门到精通04-变量
vector中函数emplace_back的实现原理
Xilinx VIVADO 中 DDR3(Naive)的使用(2)读写设计
shell之循环语句(for、while、until)
TPC藏宝计划IDO自由协议复利模式开发功能分析
Leetcode brush - structure binary tree (105. Once upon a time sequence and the sequence structure binary tree traversal sequence, 106. From the sequence with the sequence structure binary tree travers
六石编程学:编程中的直线思维与自然思维
今天15:00 | CVPR 2022 论文分享精彩继续