当前位置:网站首页>Shell script -while loop explanation
Shell script -while loop explanation
2022-07-01 08:48:00 【Little snail's way】
while Cycle is Shell The simplest kind of loop in a script , When the conditions are met ,while Execute a set of statements repeatedly , When conditions are not met , Quit while loop .
Usage is as follows :
while condition
do
statements
done
condition Express judgment condition ,statements Indicates the statement to be executed ( There can be only one , There can be more than one ),do and done All are Shell Keywords in .
Code 1: Calculate from 1 Add to 100 And .
#!/bin/bash
i=1
sum=0
while ((i <= 100))
do
((sum += i))
((i++))
done
echo "The sum is: $sum"
Output :
The sum is: 5050
stay while In circulation , As long as the judgment conditions are true , The loop will execute . For this code , As long as the variable i Is less than or equal to 100, The cycle will continue . Each loop gives a variable sum Add variables i Value , Then give the variable i Add 1, Until variable i The value is greater than 100, The cycle stops .
i++ Statement makes i Gradually increase the value of , Make the judgment conditions more and more close to “ Don't set up ”, Finally exit the loop .
Code 2: Calculate from m Add to n Value .
#!/bin/bash
read m
read n
sum=0
while ((m <= n))
do
((sum += m))
((m++))
done
echo "The sum is: $sum"
Output :
1
100
The sum is: 5050
Code 3: Implement a simple addition calculator , The user enters a number per line , Calculate the sum of all numbers .
#!/bin/bash
sum=0
echo " Please enter the number you want to calculate , Press Ctrl+D Key combination ends reading "
while read n
do
((sum += n))
done
echo "The sum is: $sum"
Output :
Please enter the number you want to calculate , Press Ctrl+D Key combination ends reading
333
444
111
The sum is: 888
Ctrl+D Composite key : Read data in the terminal , It can be equivalent to reading data in a file , Press down Ctrl+D The key combination indicates reading to the end of the file stream , here read Will fail to read , Get a non 0 The exit state of the value , Thus, the judgment conditions are not tenable , End of cycle .
边栏推荐
- 中断与其他函数共享变量、临界资源的保护
- View drawing process analysis
- FreeRTOS learning easy notes
- 如何做好固定资产管理?易点易动提供智能化方案
- 安装Oracle EE
- Dynamic proxy
- What is the material of 16mo3 steel plate? What is the difference between 16mo3 and Q345R?
- Matlab tips (23) matrix analysis -- simulated annealing
- Personal decoration notes
- 基础:3.opencv快速入门图像和视频
猜你喜欢
![Matlab [function derivation]](/img/ba/9fb9da8a458d0c74b29b21a17328fc.png)
Matlab [function derivation]

《微机原理》—总线及其形成

Guidelines and principles of did

19Mn6 German standard pressure vessel steel plate 19Mn6 Wugang fixed binding 19Mn6 chemical composition

Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process

Foundation: 2 The essence of image

Computer tips

Nacos - 配置管理

AVL树的理解和实现

集团公司固定资产管理的痛点和解决方案
随机推荐
Matlab tips (23) matrix analysis -- simulated annealing
Shell脚本-echo命令 转义符
[MFC development (17)] advanced list control list control
Personal decoration notes
软件工程师面试刷题网站、经验方法
Graduation season, I want to tell you
如何解决固定资产管理和盘点的难题?
Matlab [function derivation]
Shell脚本-select in循环
What is the material of 16MnDR, the minimum service temperature of 16MnDR, and the chemical composition of 16MnDR
I would like to know the process of stock registration and account opening by mobile phone? In addition, is it safe to open a mobile account?
《单片机原理及应用》-片外拓展
【MFC开发(17)】高级列表控件List Control
JCL and slf4j
What is 1cr0.5mo (H) material? 1cr0.5mo (H) tensile yield strength
Properties of 15MnNiNbDR low temperature vessel steel, Wugang 15MnNiDR and 15MnNiNbDR steel plates
Shell脚本-while循环详解
AVL树的理解和实现
What is the material of 15CrMoR, mechanical properties and chemical analysis of 15CrMoR
Model and view of QT