当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
多表查询
Redis(一)安装与配置
IBM Q复制ALTER ADD COLUMN
化繁为简!阿里新产亿级流量系统设计核心原理高级笔记(终极版)
临床研究方法学,到现场,到数据真实发生的地方 | 对话数智 x 张维拓
200ppi转以太网通过4Gwifi在医药设备移动平台(平板电脑、手机)
Disc burning steps
数据库表列类型;DML_添加数据;DDL_修改,删除数据库表
不会还有人不知道防抖吧?
Leetcode刷题——构造二叉树(105. 从前序与中序遍历序列构造二叉树、106. 从中序与后序遍历序列构造二叉树)
Xilinx VIVADO 中 DDR3(Naive)的使用(3)仿真测试
七夕还没选好礼物,快送这套美妆秘籍,保准没错~~
剑指offer专项突击版第19天
基于BiLSTM的回归预测方法
知乎数据分析训练营
力扣解法汇总1403-非递增顺序的最小子序列
Leetcode刷题——543. 二叉树的直径、617. 合并二叉树(递归解决)
深度学习------pytorch实现cifar10数据集
涨姿势了!原来这才是多线程正确实现方式
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