当前位置:网站首页>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.
边栏推荐
- Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
- 一名优秀的软件测试人员,需要掌握哪些技能?
- Node -- egg creates a local file access interface
- cookie、session、tooken
- Kuberntes cloud native combat high availability deployment architecture
- How to reflect and solve the problem of bird flight? Why are planes afraid of birds?
- [JS download files through url]
- Global and Chinese market of picture archiving and communication system (PACS) 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese markets for distributed generation and energy storage in telecommunications networks 2022-2028: Research Report on technology, participants, trends, market size and share
- If the browser is accidentally closed, how does react cache the forms filled out by users?
猜你喜欢
测试人进阶技能:单元测试报告应用指南
You probably haven't noticed the very important testing strategy in your work
Leetcode skimming: stack and queue 04 (delete all adjacent duplicates in the string)
excel数据透视表
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
Leetcode skimming: binary tree 01 (preorder traversal of binary tree)
Friends circle community program source code sharing
Review notes of compilation principles
JMeter做接口测试,如何提取登录Cookie
【微信授权登录】uniapp开发小程序,实现获取微信授权登录功能
随机推荐
[cascade classifier training parameters] training Haar cascades
【CTF】bjdctf_2020_babystack2
2022 pinduoduo details / pinduoduo product details / pinduoduo SKU details
How to determine whether the current script is in the node environment or the browser environment?
Global and Chinese markets for freight and logistics 2022-2028: Research Report on technology, participants, trends, market size and share
@Valid parameter verification does not take effect
gradle
Global and Chinese market of ancillary software 2022-2028: Research Report on technology, participants, trends, market size and share
SQL injection for Web Security (2)
Global and Chinese markets of beverage seasoning systems 2022-2028: Research Report on technology, participants, trends, market size and share
How to type spaces in latex
[WesternCTF2018]shrine writeup
Leetcode skimming: stack and queue 02 (realizing stack with queue)
Mitsubishi PLC FX3U pulse axis jog function block (mc_jog)
With the acquisition of Xilinx, AMD is more than "walking on two legs" | Jiazi found
[bottom pop-up selector] uniapp picker component - scroll selector popped up at the bottom
[CTF] bjdctf 2020 Bar _ Bacystack2
Global and Chinese markets of digital crosspoint switches and mux/demux 2022-2028: Research Report on technology, participants, trends, market size and share
【八大排序①】插入排序(直接插入排序、希尔排序)
[eight sorting ③] quick sorting (dynamic graph deduction Hoare method, digging method, front and back pointer method)