当前位置:网站首页>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 .
边栏推荐
- 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?
- 挖财打新股安全吗
- What is the material of 16MnDR, the minimum service temperature of 16MnDR, and the chemical composition of 16MnDR
- TypeError: __ init__ () got an unexpected keyword argument ‘autocompletion‘
- Matlab [function derivation]
- 《微机原理》—总线及其形成
- 固定资产管理系统让企业动态掌握资产情况
- FreeRTOS学习简易笔记
- 又到年中,固定资产管理该何去何从?
- Advanced level of C language pointer (Part 1)
猜你喜欢

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

Memory size end

Nacos - 服务发现

Audio-AudioRecord create(一)

Redis——Lettuce连接redis集群
V79.01 Hongmeng kernel source code analysis (user mode locking) | how to use the fast lock futex (Part 1) | hundreds of blogs analyze the openharmony source code

Audio audiorecord create (I)

TV size and viewing distance

factory type_ Id:: create process resolution

C语言指针的进阶(上篇)
随机推荐
FreeRTOS学习简易笔记
Advanced level of C language pointer (Part 1)
Nacos - 服务发现
明明设计的是高带宽,差点加工成开路?
C语言学生信息管理系统
MD文档中插入数学公式,Typora中插入数学公式
如何一站式高效管理固定资产?
Computer tips
《单片机原理及应用》-片外拓展
大型工厂设备管理痛点和解决方案
Programming with C language: calculate with formula: e ≈ 1+1/1+ 1/2! …+ 1/n!, Accuracy is 10-6
The meaning of yolov5 training visualization index
DataBinding源码分析
Shell脚本-read命令:读取从键盘输入的数据
MATLAB【函数求导】
又到年中,固定资产管理该何去何从?
ARM v7的体系结构A、R、M区别,分别应用在什么领域?
What is the material of 16mo3 steel plate? What is the difference between 16mo3 and Q345R?
任务、线程、进程 区别
《单片机原理及应用》—定时器、串行通信和中断系统