当前位置:网站首页>Shell script learning notes - circular statements
Shell script learning notes - circular statements
2022-04-22 08:41:00 【XL's Princess】
8、 ... and 、shell Process control -for Loop statement
When the script executes a task , There will always be times when circular execution is needed , For example, we need the script to execute every five minutes ping The operation of , Except for planning tasks , We can also use scripts to do , So we use the loop statement .
1、for Loop Introduction
Many people put for A cycle is called a recommendation cycle , perhaps for i in . In fact, what the former says is for Characteristics of ,for The number of given conditions is proportional to the number of given cycles , You give 5 Conditions , He just circulates 5 Time , The latter says for The grammar of .
Cycle advantages , Save memory ; Structure is clearer ; Save time and cost
2、for grammar
2.1.for grammar _one
for var in value1 value2 value2 …
do
commands
done
Next , Instance output 1-9
[root@localhost ssh]# cat shell_for.sh
#!/bin/bash
#seq You can output numbers with specified rules , By default, you can add 1; If you need to output 1——9, Every time add 2 Words , Then you can `1 2 9`; If you need to output 9-1, Then you can `9 -1 1`;
for i in `seq 1 9`
do
echo $i
# every other 1 Output once per second
sleep 1
done
[root@localhost ssh]# sh shell_for.sh
1
2
3
4
5
6
7
8
9
[root@localhost ssh]# cat shell_for_test.sh
#!/bin/bash
for var in jjl\'s is cool, jjl\'s is nice
do
echo "word: $var"
done
[root@localhost ssh]# sh shell_for_test.sh
word: jjl's word: is word: cool, word: jjl's
word: is
word: nice
2.2.for grammar _two
c Type for command
for (( Variable ; Conditions ; Self increasing and decreasing operation ))
do
Code block
done
Code demonstration
[root@localhost ssh]# cat shell_for_two.sh
#!/bin/bash
# i++ Express i=i+1
for ((i=1;i<10;i++))
do
echo $i
done
[root@localhost ssh]# sh shell_for_two.sh
1
2
3
4
5
6
7
8
9
Multiple conditions for loop
[root@localhost ssh]# cat shell_for_more.sh
#!/bin/bash
for ((a=0,b=9;a<10;a++,b--))
do
echo $a $b
done
[root@localhost ssh]# sh shell_for_more.sh
0 9
1 8
2 7
3 6
4 5
5 4
6 3
7 2
8 1
9 0
for Infinite loop Use ((;;)) Condition can realize infinite loop
[root@localhost ssh]# cat shell_for_del.sh
#!/bin/bash
for ((;;))
do
echo 'hahah'
done
3、 Loop control statement
3.1 sleep N The script executes to this step N second
Code demonstration :
#!/bin/bash
for i in `seq 9 -1 1`
do
echo -n -e "\b$i"
# every other 1 Output once per second
sleep 1
done
echo
[root@localhost ssh]# sh sleep.sh
count down :1
Monitor whether the host is alive
[root@localhost useful]# cat control_active.sh
#!/bin/bash
# Script to monitor the survival of the host
for ((;;))
do
ping -c1 $1 &>/dev/null
if [ $? -eq 0 ]
then
echo -e "`date +"%F %H:%M:%S"`: $1 is \033[32m UP \033[0m"
else
echo -e "`date +"%F %H:%M:%S"`: $1 is \033[31m Down \033[0m"
fi
# Script rhythm control More than one minute is recommended in production
sleep 5
done
[root@localhost useful]# sh control_active.sh 192.168.104.44
2021-12-24 16:28:17: 192.168.104.44 is UP
2021-12-24 16:28:22: 192.168.104.44 is UP
3.2 continue Skip one of the loops , Continue with next cycle
Default loop output 1-9, But use continue Skip output 5
[root@localhost ssh]# cat continue.sh
#!/bin/bash
# Default loop output 1-9, But use continue Skip output 5
for ((i=1;i<10;i++))
do
if [ $i -eq 5 ]
then
continue
else
echo $i
fi
done
[root@localhost ssh]# sh continue.sh
1
2
3
4
6
7
8
9
3.3 break Jump out of the loop and continue to execute subsequent code
[root@localhost ssh]# cat for_break.sh
#!/bin/bash
# Ask the user to enter a letter Q, When the input Q To exit the loop
for ((;;))
do
read -p "char:" ch
if [ $ch == "Q" ]
then
echo " Input correct "
break
else
echo " The letter you typed is wrong , The letter you entered is :$ch"
fi
done
[root@localhost ssh]# sh for_break.sh
char:S
The letter you typed is wrong , The letter you entered is :S
char:E
The letter you typed is wrong , The letter you entered is :E
char:R
The letter you typed is wrong , The letter you entered is :R
char:W
The letter you typed is wrong , The letter you entered is :W
char:Q
Input correct
Nine 、shell Process control -while Loop statement
One 、 Use scenarios
Clearly know the number of cycles, use for, Use when you don't know the number of cycles while
Two 、while Cyclic grammar
while [ condition ] # Be careful : Condition is true while Will be recycled , The condition is false ,while Stop the cycle ,** If there is a variable in the condition, enclose the variable name in quotation marks **.
do
commands
done
3、 ... and 、 example
#!/bin/bash
#while Basic grammar learning
# The condition can be any of the five kinds of transportation we learn
# Mathematics compares transportation String comparison file type Logical transportation The assignment operation
# Enter a number , If this number is greater than 0, Then output “ Greater than ”
read -p "NUM:" num1
while [ $num1 -gt 0 ]
do
echo " Greater than "
sleep 3
done
# Enter a string , If the input string is “root“, Then jump out of the loop
read -p "login:" account
while [ $account != 'root' ]
do
read -p "login:" account
done
# If home There is no jjl Folder , Then it will always output “not found /home/jjl”
while [ ! -d /home/jjl ]
do
echo "not found /home/jjl"
sleep 3
done
# If money Less than 100000,car_num Less than 1,house Less than 1, Then continue the cycle
read -p "money:" money
read -p "car:" car_num
read -p "house:" house
while [ $money -lt 100000 ] || [ $car_num -lt 1 ] || [ $house -lt 1 ]
do
echo "not"
read -p "money:" money
read -p "car:" car_num
read -p "house:" house
done
echo "ok"
Ten 、shell Process control -until Loop statement
One 、until Introduce
and while Just the opposite ,until If the condition is false, start execution , If the condition is true, stop execution
Two 、until grammar
until [ condition ] # Be careful : The condition is false until Just start the cycle , If the condition is true, stop the cycle
do
commands Code block
done
版权声明
本文为[XL's Princess]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220741455003.html
边栏推荐
猜你喜欢
随机推荐
mysql插入自由列
Disk management - raid creation
The PS - EF query process PID in the shell script always returns an exception
cesium 采集地形高度,采集模型高度 (异步方法,适合数据较多的时候)
JS judge the element to the top and fix it
94. 二叉树的中序遍历(Easy)
jmu-枚举WeekDay
链表打印(链表反转输出)
Seven crimes of hackers in social engineering -- greed (Ping of death)
cesium中实现楼层分解动画
win10安装Mongo出错Service ‘MongoDB Server’ failed to start
Level 1: create / delete nodes
大一的建议
cesium鼠标拾取要素,并判断要素类别
Freshman advice
Installation de MySQL par CentOS
Seven crimes of hackers in social engineering -- hooking
SQL 語句中 “意想不到” 的操作
Monkey eating peach problem (loop, recursion)
社会工程学之黑客七宗罪——傲慢(Hooking)









