当前位置:网站首页>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.
边栏推荐
- 2022 pinduoduo details / pinduoduo product details / pinduoduo SKU details
- XMind思维导图
- Advanced skills of testers: a guide to the application of unit test reports
- Collection: comprehensive summary of storage knowledge
- Leetcode question brushing: stack and queue 07 (maximum value of sliding window)
- Promise和模块块化编程
- XMIND mind map
- 【八大排序①】插入排序(直接插入排序、希尔排序)
- 教你白嫖Amazon rds一年并搭建MySQL云数据库(只需10分钟,真香)
- Global and Chinese markets for power over Ethernet (POE) solutions 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

How do Lenovo computers connect Bluetooth headsets?

2022 pinduoduo details / pinduoduo product details / pinduoduo SKU details

DTL dephossite | prediction method of dephosphorylation sites based on Transfer Learning

The 8-year salary change of testers makes netizens envy it: you pay me one year's salary per month
![[eight sorts ①] insert sort (direct insert sort, Hill sort)](/img/8d/2c45a8fb582dabedcd2658cd7c54bc.png)
[eight sorts ①] insert sort (direct insert sort, Hill sort)
![[wechat authorized login] the small program developed by uniapp realizes the function of obtaining wechat authorized login](/img/c1/23be4399119f42d85a7b86fc8a59fc.png)
[wechat authorized login] the small program developed by uniapp realizes the function of obtaining wechat authorized login

工作中非常重要的测试策略,你大概没注意过吧

To meet the needs of consumers in technological upgrading, Angel water purifier's competitive way of "value war"

gradle
![[eight sorts ④] merge sort, sort not based on comparison (count sort, cardinal sort, bucket sort)](/img/0d/22f3f65ab9422383df9a55d0724d59.jpg)
[eight sorts ④] merge sort, sort not based on comparison (count sort, cardinal sort, bucket sort)
随机推荐
Creation of volume group for AIX storage management (I)
Output results of convolution operation with multiple tensors and multiple convolution kernels
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
【八大排序①】插入排序(直接插入排序、希尔排序)
使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)
Common loss function of deep learning
2022 safety officer-a certificate examination questions and online simulation examination
Tensorflow tensor convolution, input and convolution kernel dimension understanding
How to determine whether the current script is in the node environment or the browser environment?
【八大排序③】快速排序(动图演绎Hoare法、挖坑法、前后指针法)
How can programmers better plan their career development?
首场“移动云杯”空宣会,期待与开发者一起共创算网新世界!
关于ASP.NET CORE使用DateTime日期类型参数的一个小细节
Promise and modular programming
Bc35 & bc95 onenet mqtt (old)
Global and Chinese markets of beverage seasoning systems 2022-2028: Research Report on technology, participants, trends, market size and share
[bottom pop-up selector] uniapp picker component - scroll selector popped up at the bottom
Excel PivotTable
@Valid parameter verification does not take effect
ACM教程 - 快速排序(常规 + 尾递归 + 随机基准数)