当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Glitch free clock switching technology

Properties of 15MnNiNbDR low temperature vessel steel, Wugang 15MnNiDR and 15MnNiNbDR steel plates

Computer tips

《微机原理》——微处理器内部及外部结构

Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched

1.jetson与摄像头的对接

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

【面试必刷101】链表

15Mo3 German standard steel plate 15Mo3 chemical composition 15Mo3 mechanical property analysis of Wuyang Steel Works

电脑小技巧
随机推荐
C语言学生信息管理系统
如何高效拉齐团队认知
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?
Foundation: 2 The essence of image
Do you know how data is stored? (C integer and floating point)
It technology ebook collection
Glitch free clock switching technology
又到年中,固定资产管理该何去何从?
View drawing process analysis
Promise异步编程
Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process
Shell脚本-while循环详解
ARM v7的体系结构A、R、M区别,分别应用在什么领域?
1.jetson与摄像头的对接
15Mo3 German standard steel plate 15Mo3 chemical composition 15Mo3 mechanical property analysis of Wuyang Steel Works
Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting
The meaning of yolov5 training visualization index
Ape anthropology topic 20 (the topic will be updated from time to time)
Vscode customize the color of each area
[MFC development (16)] tree control