当前位置:网站首页>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.
边栏推荐
- Creating logical volumes and viewing and modifying attributes for AIX storage management
- Comprehensive broadcast of global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
- [WesternCTF2018]shrine writeup
- 【八大排序③】快速排序(动图演绎Hoare法、挖坑法、前后指针法)
- gradle
- Recently, three articles in the nature sub Journal of protein and its omics knowledge map have solved the core problems of biology
- AIX存储管理之卷组的创建(一)
- 2023 Lexus ES products have been announced, which makes great progress this time
- PLC Analog input analog conversion FB s_ ITR (Mitsubishi FX3U)
- 测试员8年工资变动,令网友羡慕不已:你一个月顶我一年工资
猜你喜欢

Entrepreneurship is a little risky. Read the data and do a business analysis

Leetcode skimming: stack and queue 01 (realizing queue with stack)

"C zero foundation introduction hundred knowledge hundred examples" (73) anonymous function -- lambda expression

You probably haven't noticed the very important testing strategy in your work

How does schedulerx help users solve the problem of distributed task scheduling?

【八大排序③】快速排序(动图演绎Hoare法、挖坑法、前后指针法)

Weather forecast applet source code weather wechat applet source code

gradle

Use es to realize epidemic map or take out order function (including code and data)

教你白嫖Amazon rds一年并搭建MySQL云数据库(只需10分钟,真香)
随机推荐
King combat power query renamed toolbox applet source code - with traffic main incentive advertisement
Source code of Qiwei automatic card issuing system
Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
449 original code, complement code, inverse code
How to extract login cookies when JMeter performs interface testing
Weather forecast applet source code weather wechat applet source code
AIX存储管理之逻辑卷的创建及属性的查看和修改
【CTF】bjdctf_2020_babystack2
[cascade classifier training parameters] training Haar cascades
【八大排序②】选择排序(选择排序,堆排序)
XMind思维导图
Some understandings of graph convolution neural network r-gcn considering relations and some explanations of DGL official code
To meet the needs of consumers in technological upgrading, Angel water purifier's competitive way of "value war"
S32Kxxx bootloader之UDS bootloader
什么是商业养老保险?商业养老保险安全靠谱吗?
首场“移动云杯”空宣会,期待与开发者一起共创算网新世界!
The 8-year salary change of testers makes netizens envy it: you pay me one year's salary per month
[eight sorts ②] select sort (select sort, heap sort)
Han Zhichao: real time risk control practice of eBay based on graph neural network
@Valid参数校验不生效