当前位置:网站首页>Circular statements in shell programming
Circular statements in shell programming
2022-07-02 01:03:00 【Hadron's blog】
The function of the loop statement
In practice , 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 , Clear according to the server Single check the survival status of each host. When facing repeated tasks in various lists , Easy to use if Statement has been difficult to meet the requirements , And write all generations in sequence The code is extremely cumbersome 、 The difficulties
One ,for loop
1、 Grammatical structure
(1) List loop
(2) Loop without list
(3) class C Style for loop
Traverse
for Variable name in {list}
do
command
done
for i in {a..c}
do
echo $i
done
for i in {1..5} //{} Variables are not recognized inside
do
echo $i
done
for i in `seq 5` //seq 5=1、2、3、4、5 Sequence
do
echo $i
done
a=10
for i in `seq $a` //seq You can refer to variables
do
echo $i
done
for Variable name in a b c
do
command
done
for i in a b c //i There is actually no call here , So it's equivalent to in The following parameters will be cycled several times
do
echo 123
done
for i in a b c // Here we call the i The variable , So it displays normally i Value (a\b\c)
do
echo $i
done
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
2. skill
for i in {1..50..2} 1-50 The odd number
for i in {2..50..2} 1-50 An even number of
for i in {10..1} 1-10 Reverse order
for i in $(seq 10) 1-10 Positive order
for i in $(seq 10 -1 1) 1-10 Reverse order
for i in $(seq 1 2 10) 1-10 The odd number , The middle is the step size
for i in $(seq 0 2 10) 1-10 An even number of , The middle is the step size
Example :
for i in $(seq 0 2 10);do echo $i ;done
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
3, Example
1. Print 1-5 this 5 A digital
vim for.sh
#!/bin/bash
for i in {1..5}
do
echo $i
done
2. Print 5 Time hello world
Be careful : Although we define a variable i, But I didn't use it , It just controls the number of cycles
vim for.sh
#!/bin/bash
for i in {1..5}
do
echo hello world
done
3. Output 0-50 Even number between
vim for1.sh
#!/bin/bash
for i in {0..50..2} //..2 The representative step size is 2, every other 2 individual
do
echo $i
done
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
4. class C Style for loop
for ((expr1;expr2;expr3))
do
command
done
expr1: Define variables and assign initial values
expr2: Decide whether to cycle or not
expr3: Decide how the loop variable changes , Decide when the loop will exit
1. Print 1-5
iteration
vim for.sh
#!/bin/bash
for ((i=1;i<=5;i++))
do
echo $i
done
2. Print 1-10 The odd number
[[email protected] ~]# vim for3.sh
#!/bin/bash
for ((i=1;i<=10;i+=2)) //i=i+2
do
echo $i
done
3. Script batch add users =====
#!/bin/bash
ULIST=$(cat /root/users.txt)
for UNAME in $ULIST
do
useradd $UNAME
echo "123456" | passwd --stdin $UNAME &>/dev/null
done
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
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
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
1. Grammatical structure :
while expression
do
command
done
1. Print 1-5
vim while.sh
#!/bin/bash
i=1
while [ $i -le 5 ]
do
echo $i
let i++ // Note that if this doesn't change $i Value , It's going to be a dead cycle
# i=$[$i+1] // Two ways of writing
done
echo " Last i The value of is : $i"
2. Output 1-100 Can't be 3 Divisible numbers
vim 3.sh
#!/bin/bash
i=1
while [ $i -le 100 ]
do
if [[ $i%3 -ne 0 ]]
then echo "$i"
fi
let i++
done
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
2.while Dead cycle
while [ 1 -eq 1 ] // Write an expression that is always true ,1 be equal to 1 This condition is always true , So this script will continue to loop
do
command
done
while true
do
command
done
while :
do
command
done
Guess the number , Guess wrong and keep guessing
num=10
while true
do
read -p " Please enter a number :" shu
if [ $shu -eq $num ];then
echo " You guessed it "
break
elif [ $shu -gt $num ];then
echo " You guessed big "
elif [ $shu -lt $num ];then
echo " You guess it's small "
fi
done
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
3、 ... and ,until loop
1. 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
2. Dead cycle structure
until false
do
command
done
until [ 1 -ne 1 ]
do
command
done
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 I won't execute the following code in the loop body , Start and restart the next cycle
example 1: Print 1-5 The number of ,3 No printing
vim for4.sh
#!/bin/bash
for ((i=1;i<=5;i++))
do
if [ $i -eq 3 ];then
continue
else
echo $i
fi
done
The result is 1245,3 No output , Because jump out of the back echo Statement executes the next loop
2、break
interrupt , Stop this cycle immediately , Execute the extracorporeal code
example 2:1-10 The number of ,7 The back ones don't print
vim for4.sh
#!/bin/bash
for ((i=1;i<=10;i++))
do
if [ $i -eq 8 ];then
break
else
echo $i
fi
done
3、exit
Jump out of the program , It can be followed by a status return code, such as exit 1 wait
for i in {1..5}
do
if [ $i -eq 3 ];then
exit 100
else
echo $i
fi
done
echo hi
Jump out of the program directly, so the final... Will not be executed echo hi, And the return code is 100 adopt $? see
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
边栏推荐
- cookie、session、tooken
- Global and Chinese market of wireless chipsets 2022-2028: Research Report on technology, participants, trends, market size and share
- What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
- Geek DIY open source solution sharing - digital amplitude frequency equalization power amplifier design (practical embedded electronic design works, comprehensive practice of software and hardware)
- Schrodinger's Japanese learning applet source code
- [cascade classifier training parameters] training Haar cascades
- How does schedulerx help users solve the problem of distributed task scheduling?
- Promise和模块块化编程
- XMind思维导图
- How to extract login cookies when JMeter performs interface testing
猜你喜欢

The pain of Xiao Sha

Collection: comprehensive summary of storage knowledge

Synthetic watermelon game wechat applet source code / wechat game applet source code

Barbie q! How to analyze the new game app?

教你白嫖Amazon rds一年并搭建MySQL云数据库(只需10分钟,真香)

AIX存储管理之卷组属性的查看和修改(二)

Develop a simple login logic based on SSM

【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)

Xinniuniu blind box wechat applet source code_ Support flow realization, with complete material pictures

Leetcode skimming: stack and queue 05 (inverse Polish expression evaluation)
随机推荐
Comprehensive broadcast of global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
2022 operation of simulated examination platform for melting welding and thermal cutting work license
【八大排序②】选择排序(选择排序,堆排序)
Iclr2022 | spherenet and g-spherenet: autoregressive flow model for 3D molecular graph representation and molecular geometry generation
【CTF】bjdctf_ 2020_ babystack2
[wechat authorized login] the small program developed by uniapp realizes the function of obtaining wechat authorized login
The 8-year salary change of testers makes netizens envy it: you pay me one year's salary per month
测试人进阶技能:单元测试报告应用指南
Powerful calendar wechat applet source code - support the main mode of doing more traffic
How to extract login cookies when JMeter performs interface testing
Source code of Qiwei automatic card issuing system
[WesternCTF2018]shrine writeup
Export default the exported object cannot be deconstructed, and module Differences between exports
sso单点登录的实现。
Output results of convolution operation with multiple tensors and multiple convolution kernels
程序员该如何更好的规划自己的职业发展?
Global and Chinese market of avionics systems 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for power over Ethernet (POE) solutions 2022-2028: Research Report on technology, participants, trends, market size and share
AIX存储管理之卷组属性的查看和修改(二)
SQL injection for Web Security (2)