a while loop
Syntax while condition; do … ; doneCase
1: Check the system load every 30s, and send an email when the system load is greater than 10
Analysis: If we don't use the while loop, we can only use the task schedule cron, but the minimum time is 1 minute, which does not meet the requirement of checking every 30s
#!/bin/bash
while : //这里的:冒号可以用true代替,写的是死循环
do
load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1` //给负载load赋值
if [ $load -gt 10 ]
then
top|mail -s "load is high: $load" [email protected]
fi
sleep 30
done
Like this infinite loop script, in order not to let it terminate unexpectedly, we can open a screen to run it
. Let's parse the command in the above case w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1
[root@lijie-01 ~]# w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1
0
[root@lijie-01 ~]# w //查看负载
06:56:07 up 13:59, 2 users, load average: 0.05, 0.03, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.75.1 04:52 10:39 0.66s 0.40s vim while.sh
root pts/2 192.168.75.1 05:57 7.00s 0.10s 0.00s w
[root@lijie-01 ~]# uptime //截取负载信息的第一行
06:56:12 up 13:59, 2 users, load average: 0.04, 0.03, 0.05
[root@lijie-01 ~]# w|head -1 //效果等同于上面一条命令
07:04:21 up 14:07, 2 users, load average: 0.00, 0.01, 0.05
[root@lijie-01 ~]# uptime|awk -F 'load average:' '{print $2}' //以load average:作为分隔符截取负载信息第一行的第二段
0.02, 0.02, 0.05
[root@lijie-01 ~]# uptime|awk -F 'load average:' '{print $2}'|cut -d. -f1 //再以.作为分隔符截取第一段
0
[root@lijie-01 ~]# uptime|awk -F 'load average: ' '{print $2}'|cut -d. -f1 //load average: 后面加上空格以去掉上面一条命令运行结果前面的0
0
[root@lijie-01 ~]# uptime|awk -F 'load average:' '{print $2}'|cut -d. -f1|sed 's/ //' //也可以再用 sed 's/ //'过滤一次以去掉上面一条命令运行结果前面的0
0
We can also write
#!/bin/bash
while true
do
load='w|head -1|awk -F `load average: ` `{print $2}` |cut -d. -f1'
if [ $load -gt 10 ]
then
/usr/lib/zabbix/alertscripts/mail.py [email protected] "load high" " $load"
fi
sleep 30
done
Error in running result
[root@lijie-01 shell]# sh while.sh
while.sh: 第 5 行:[: 参数太多
Let's troubleshoot the error and find that there is a problem with the quotation marks in the load assignment statement. After modification, the following error is displayed
[root@lijie-01 shell]# sh while.sh
while.sh:行4: average:: 未找到命令
while.sh: 第 5 行:[: -gt: 期待一元表达式
^[while.sh:行4: average:: 未找到命令
while.sh: 第 5 行:[: -gt: 期待一元表达式
while.sh:行4: average:: 未找到命令
while.sh: 第 5 行:[: -gt: 期待一元表达式
It turns out that the load assignment statement is a system command, the outermost layer should use backticks , and then run after modification. Since we wrote an infinite loop, the script is always running, as follows:
[root@lijie-01 shell]# sh while.sh
In order to observe the phenomenon more clearly, we modify the script to send emails when the load is less than 10
[root@lijie-01 shell]# cat !$
cat while.sh
#!/bin/bash
while :
do
load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1`
if [ $load -lt 10 ]
then
/usr/lib/zabbix/alertscripts/mail.py [email protected] "load high" " $load"
fi
exit
done
After 30s, the email is normally received
while loop Case 2: Make a script that allows the user to keep inputting what we want
[root@lijie-01 shell]# cat !$
cat while2.sh
#!/bin/bash
while :
do
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "你还没有输入的,请输入!"
continue
fi
n1=`echo $n|sed 's/[0-9]//g'`
if [ ! -z "$n1" ]
then
echo "你需要输入一个纯数字!"
continue
fi
break
done
echo $n
to execute the following
[root@lijie-01 shell]# sh -x while2.sh
+ :
+ read -p 'Please input a number: ' n
Please input a number:
+ '[' -z '' ']'
+ echo '你还没有输入的,请输入!'
你还没有输入的,请输入!
+ continue
+ :
+ read -p 'Please input a number: ' n
Please input a number: 12324dfff
+ '[' -z 12324dfff ']'
++ sed 's/[0-9]//g'
++ echo 12324dfff
+ n1=dfff
+ '[' '!' -z dfff ']'
+ echo '你需要输入一个纯数字'
你需要输入一个纯数字
+ continue
+ :
+ read -p 'Please input a number: ' n
Please input a number: 3425
+ '[' -z 3425 ']'
++ sed 's/[0-9]//g'
++ echo 3425
+ n1=
+ '[' '!' -z '' ']'
+ break
+ echo 3425
3425
[root@lijie-01 shell]#
Two break out of the loop
break is used in a loop statement
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i == 3 ] //如果是数字间的比较,可以使用-eq;如果比较的对象包含数字外的其他字符,则必须要使用==来比较
then
break
fi
echo $i
done
echo abc
The execution process is as follows:
[root@lijie-01 shell]# sh -x break.sh
++ seq 1 5
+ for i in '`seq 1 5`'
+ echo 1
1
+ '[' 1 == 3 ']'
+ for i in '`seq 1 5`'
+ echo 2
2
+ '[' 2 == 3 ']'
+ for i in '`seq 1 5`'
+ echo 3
3
+ '[' 3 == 3 ']'
+ break
+ echo abc
abc
[root@lijie-01 shell]#
Three continue to end this cycle
Ignore the code under continue and go directly to the next loop
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i == 3 ]
then
continue //当i=3时,跳出本次循环
fi
echo $i
done
echo abc
The execution process is as follows:
[root@lijie-01 shell]# sh continue.sh
1
1
2
2
3
4
4
5
5
abc
[root@lijie-01 shell]#
Four exit to exit the entire script
[root@lijie-01 shell]# cat !$
cat exit.sh
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i == 3 ]
then
exit 2
fi
echo $i
done
echo abc
[root@lijie-01 shell]# sh exit.sh
1
1
2
2
3
[root@lijie-01 shell]# echo $?
2
[root@lijie-01 shell]#