当前位置:网站首页>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 .
边栏推荐
- 猿人学第20题(题目会不定时更新)
- Shell脚本-case in语句
- 易点易动助力企业设备高效管理,提升设备利用率
- Shell脚本-位置参数(命令行参数)
- How can enterprises and developers take the lead in the outbreak of cloud native landing?
- yolov5训练可视化指标的含义
- TypeError: __init__() got an unexpected keyword argument ‘autocompletion‘
- Matlab [functions and images]
- FreeRTOS学习简易笔记
- 中断与其他函数共享变量、临界资源的保护
猜你喜欢
TypeError: __ init__ () got an unexpected keyword argument ‘autocompletion‘
电脑小技巧
Matlab tips (23) matrix analysis -- simulated annealing
Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched
避免按钮重复点击的小工具bimianchongfu.queren()
"Analysis of 43 cases of MATLAB neural network": Chapter 30 design of combined classifier based on random forest idea - breast cancer diagnosis
MATLAB【函数和图像】
Embedded Engineer Interview Question 3 Hardware
一文纵览主流 NFT 市场平台版税、服务费设计
1.jetson与摄像头的对接
随机推荐
Screenshot tips
View drawing process analysis
用C语言编程:用公式计算:e≈1+1/1!+1/2! …+1/n!,精度为10-6
明明设计的是高带宽,差点加工成开路?
Shell script - definition, assignment and deletion of variables
Configuration and startup of Chang'an chain synchronization node
Full mark standard for sports items in the high school entrance examination (Shenzhen, Anhui and Hubei)
Nacos - service discovery
Shell脚本-for循环和for int循环
中小企业固定资产管理办法哪种好?
Introduction to 18mnmo4-5 steel plate executive standard and delivery status of 18mnmo4-5 steel plate, European standard steel plate 18mnmo4-5 fixed rolling
C语言学生信息管理系统
【MFC开发(17)】高级列表控件List Control
《微机原理》—总线及其形成
1.jetson与摄像头的对接
基础:3.opencv快速入门图像和视频
Matlab [functions and images]
《单片机原理及应用》—定时器、串行通信和中断系统
【C】 Summary of wrong questions in winter vacation
Shell脚本-read命令:读取从键盘输入的数据