当前位置:网站首页>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 .
边栏推荐
- jeecg 重启报40001
- Insert mathematical formula in MD document and mathematical formula in typora
- 截图小妙招
- Software Engineer Interview Question brushing website and experience method
- Share 7 books I read in the first half of 2022
- 易点易动助力企业设备高效管理,提升设备利用率
- Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting
- 又到年中,固定资产管理该何去何从?
- Screenshot tips
- Nacos - 配置管理
猜你喜欢

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

Nacos - gestion de la configuration

It is designed with high bandwidth, which is almost processed into an open circuit?

Only in China! Alicloud container service enters the Forrester leader quadrant

Vscode customize the color of each area

如何做好固定资产管理?易点易动提供智能化方案

What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?

Foundation: 2 The essence of image

Nacos - 配置管理

大型工厂设备管理痛点和解决方案
随机推荐
Shell script - positional parameters (command line parameters)
JCL and slf4j
固定资产管理系统让企业动态掌握资产情况
[MFC development (16)] tree control
Shell script -for loop and for int loop
Jeecg restart alarm 40001
Nacos - gestion de la configuration
The meaning of yolov5 training visualization index
NIO-零拷贝
Shell脚本-read命令:读取从键盘输入的数据
Shell脚本-case in语句
《单片机原理及应用》—定时器、串行通信和中断系统
Shell脚本-if else语句
为什么LTD独立站就是Web3.0网站!
挖财打新股安全吗
Shell脚本-数组定义以及获取数组元素
Principle and application of single chip microcomputer - principle of parallel IO port
猿人学第20题(题目会不定时更新)
Glitch Free时钟切换技术
Share 7 books I read in the first half of 2022