shell训练day8 8.22
2019-08-26 17:16
1346 查看
while循环
• 语法 while 条件; do … ; done
• 案例1
#!/bin/bash
while :
do
load=
w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1
if [ $load -gt 10 ]
then
top|mail -s "load is high: $load" asldkfls@11.com
fi
sleep 30
done
: 的意思就是 死循环,也可以写成1或者true
获取负载
[root@docker ~]# uptime|awk -F 'load average: ' '{print $2}'|cut -d . -f 1
0--指定分隔符时多包含了空格
[root@docker ~]# uptime|awk -F 'load average:' '{print $2}'|cut -d . -f 1
0 返回多了空格多了空格
[root@docker whiletest]# cat load.sh
#!/bin/bash
while :
do
load=
w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1
if [ $load -gt 10 ]
then
top|echo "load is high: $load"
fi
sleep 30
done
#!/bin/bash
while :
do
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "you need input sth."
continue
fi
n1=
echo $n|sed 's/[0-9]//g'
if [ -n "$n1" ]
then
echo "you just only input numbers."
continue
fi
break
done
echo $n
continue--表示继续上面的循环
n1=
echo $n|sed 's/[0-9]//g'表示一个纯数字变量
break 也可以用在for循环里
跳出循环
#!/bin/bash
for i in
seq 1 5
do
echo $i
if [ $i == 3 ]
then
break
fi
echo $i
done
echo aaaaaaa
== 比较字符串,数字一般是用eq
为什么都是2次输出呢? --do后第一个echo打印了一个1,然后判断不等于3,fi后面又打印了一次
continue的用法
结束本次循环
#!/bin/bash
for i in
seq 1 5
do
echo $i
if [ $i == 3 ]
then
continue
fi
echo $i
done
echo $i
continue就是 继续执行,而break就是重头再来
exit
#!/bin/bash
for i in
seq 1 5
do
echo $i
if [ $i == 3 ]
then
exit
fi
echo $i
done
echo aaaaaaa
相关文章推荐
- shell训练day 2
- Shell实战训练营Day8
- 纪中训练 day8 【NOIP普及组】模拟赛D组 解题报告
- shell基础DAY8
- shell 1c54 训练DAY24
- shell习题训练
- 训练总结 8.22
- SHELL训练营--day8__shell函数和数组
- shell 2229 训练DAY16
- 由浅入深shell脚本训练
- shell训练day 4 8.16
- Shell语法训练
- faster-rcnn 之 shell脚本开始训练:./experiments/scripts/faster_rcnn_alt_opt.sh 0 ZF pascal_voc
- shell训练营 Day8
- shell训练DAY23
- shell训练day15
- 【GDOI】8.22训练总结
- shell训练DAY20
- shell 脚本---每隔几个小时查看日志文件中包含某些字符串的行数
- django shell python-mysqldb