当前位置:网站首页>Shell (8) cycle
Shell (8) cycle
2022-07-27 01:42:00 【A simple comparison】
List of articles
Shell(8) loop
Preface
Mentioned in the previous chapter if Judge sentences and case Sentences can only be judged once , When we want to automatically execute this script repeatedly many times, we need to use circular statements .
There are three main types of circular statements :for loop ,while Circulation and until loop .
One 、for loop
At work , There are often situations where a task needs to be performed many times , And each time it is executed, it is only processed Different objects , The other commands are the same . for example , Create a system account according to the name list in the address book , Check the survival status of each host according to the server list .
When repeating tasks in the face of various lists , Easy to use if Statement has been difficult to meet the requirements , Writing all the code in sequence is extremely cumbersome 、 The difficulties .
for loop , Some people call it conditional circulation , perhaps for i in , In fact, that is for The nature of the cycle , The number of times is proportional to the condition of giving .

Grammatical structure :(1) List loop (2) Loop without list (3) class C Style for loop
1. List loop
for $ Variable in {list} # take list The value in is assigned to the variable in a traversal way
do
command # Use each variable to execute commands , Until it's all done
done
for i in {1..5} #{} Instead of identifying variables, it will 1 To 5 Assign to respectively i
do
echo $i # The output is 1 2 3 4 5
done
Or use seq command (seq Variables can be used after the command )
for i in `seq 5` #seq 5=1、2、3、4、5 Sequence
do
echo $i # The output is the same 1 2 3 4 5
done
for i in {1..5}
do
echo hello world
done
# Although we define a variable i, But I didn't use it , It just controls the number of cycles , So it will output 5 All over hello world
# Output 0-50 Even number between
for i in {0..50..2} #..2 The representative step size is 2, Said every 2 individual
do
echo $i
done
Or use seq command
for i in `seq 0 2 50` # From 0 Start , every other 2 individual , Until 50
do
echo $i
done
Two 、 Loop without list
When the loop without list is executed, it is determined by the parameters specified by the user and the number of parameters .
for $ Variable
do
command
done
Execute the script + Variable parameters
example 1: take {list} The parameter in is written in the command line as the number of times

hold a Assign a value to a variable i,i When it has value, it starts to execute do…done 了 .
example 2: take {list} The parameters in are written on the command line as variables

3. class C Style for loop
class C Style representation is similar to C Language , The grammatical structure is :
for ((i=1;i<=5;i++)) #i from 1 Start ,i Final less than or equal to 5,i Self add every time 1
do
echo $i # Output 1 1+1 1+1+1 1+1+1+1 1+1+1+1+1
done
The execution result is 1 2 3 4 5
notes :i++ : i=1+1 Assign first and then calculate i=1 after Again +1
++i : 1+1=i Operation before assignment 1+1 after Again =i
class C Style operator usage :
| ++ | Self variable +1 |
|---|---|
| – | Self variable -1 |
| +=n | Self variable +n |
| -=n | Self variable -n |
| *=n | Self variable *n |
| /=n | Self variable /n |
| %=n | Self variable %n |
example : To calculate the 10 Odd sum within , And print .

Two 、while loop
while Loops are generally used for conditional judgment loops , If the judgment condition is true , Then enter the cycle , When the condition is false, jump out of the loop .

Grammatical structure :
while expression
do
command
done
example : Print 1-10 And

while Dead cycle : When we write an expression that is always true ,while It will go through a cycle without jumping out , because while The criterion for ending the loop is when the result output is false . for example
while [ 1 -eq 1 ] # When 1 be equal to 1 When
do
echo "1" # Output 1
done
Once executed, this script will continue until the process is stopped .
example : use while Write a number guessing game in a circle

3、 ... and 、until loop
Follow while contrary , If the condition is false, enter the loop , If the condition is true, exit the loop .

Grammatical structure :
until expression
do
command
done
example : Show 1-10 The integer of

Four 、 Loop control statement
for Loops are generally executed together with conditional judgment statements and process control statements , Then you need to skip the loop and abort the loop , The commands to control the loop are as follows 3 individual .
1.continue: continue , But the code below this loop will not be executed , Start and restart the next cycle .
2.break: interrupt , Stop this cycle immediately , Execute code outside the loop .
3.exit: Jump out of the program , It can be followed by a status return code, such as exit 1 wait .
summary
for Generally used for fixed number of cycles , When you value the list to 4 When you get a value , Just cycle 4 Time ,5 A value is 5 Time .
while and until Generally used for non fixed number of cycles , As long as the condition test is true , It's been circulating do and done The command .
END
边栏推荐
- Big model training is difficult to go to the sky? Here comes the efficient and easy-to-use "Li Bai" model library
- [by pass] bypass method of file upload
- MySQL中对于事务完整的超详细介绍
- 31正则表达式
- [ctf real question] 2018 WANGDING cup web unfinish
- CDC only supports PostgreSQL database to version 12? Is there any plan to support version 14? Does anyone know?
- DHCP experiment ideas
- 正则表达式
- 1101: numbers in reverse order (function topic)
- Ubuntu12.10安装Mysql5.5(三)
猜你喜欢
随机推荐
七、循环语句
iptables
Shell(13)三剑客
标准C库的IO函数
Ubuntu12.10安装Mysql5.5(三)
Shell(7)case语句
4.1 It is super simple to install QT without using Sogou Chinese input method
VirtualBox VMS extended disk space
27shell conditional statement
[ctf attack and defense world] questions about cookies in the web area
4、 Operation of numerical variables
Buuctf casual note, exec, easysql, secret file
散户股票开户哪个证券公司好,哪个更安全
Understanding and learning of code blocks
DHCP实验思路
十六、awk
FTP service
Text three swordsman two
Web服务器(01)——介绍web服务器
Download pronunciation pairs+ship or sheet+tree or three
![[by pass] bypass method of WAF](/img/dd/7204b2401a9f18c02c8b9897258905.png)








