linux中的(())、()、grep、awk、tr
问题:
编写脚本判断/分区的空间是否大于80%,如果大于80%就在屏幕上输出root partition is not enough
(())
(())双圆括号用来进行整数的运算和比较
shell编程里(命令) 可以创建一个子进程去执行命令的
(( 整数的运算 ))
[root@master back]# a=10
[root@master back]# b=20
[root@master back]# ((a + b))
[root@master back]# c=((a + b)) $引用变量的值,此处没有使用$引用运算后的结果
-bash: 未预期的符号 `(' 附近有语法错误
[root@master back]# c=$((a + b))
[root@master back]# echo $c
30
# 双圆括号中使用变量可以加$也可以不加
[root@master ~]# d=$(($c+$b))
[root@master ~]# echo $d
50
双圆括号(( ))不能进行小数的运算,要想进行小数运算可以使用bc命令
[root@master ~]# abc=$(($d + 5.5 ))
-bash: 50 + 5.5 : 语法错误: 无效的算术运算符 (错误符号是 ".5 ")
[root@master ~]#
[root@master ~]# abc=$(($d + 5 ))
[root@master ~]# echo $abc
55
[root@master ~]# abc=$(($d * 5 ))
[root@master ~]# echo $abc
250
[root@master ~]#
整数的比较
通过$?查看结果是否正确
[root@master ~]# ((80 > 50))
[root@master ~]# echo $?
0
[root@master ~]# ((80 > 500))
[root@master ~]# echo $?
1
[root@master ~]#
文本处理三剑客
1.grep 过滤
2.awk 截取
3.sed 替换
grep
grep 是一个文本过滤的命令
[root@lb1 backup]# cat /etc/passwd|grep dengchao
dengchao:x:1001:1001::/home/dengchao:/bin/bash
[root@lb1 backup]#
grep -v
过滤出没有指定字符串的指令
[root@lwq network-scripts]# arp -a|grep -v 'incomplete'
? (192.168.1.2) at 00:50:56:f8:df:64 [ether] on eno16777736
? (192.168.1.1) at 00:50:56:c0:00:08 [ether] on eno16777736
? (192.168.1.254) at 00:50:56:f5:ff:5d [ether] on eno16777736
grep -o
只过滤出指定的字符串,并且单独成行
[root@lwq network-scripts]# cat /etc/passwd|grep -o 'bash'
bash
bash
bash
bash
bash
bash
bash
bash
bash
bash
bash
bash
bash
bash
bash
bash
bash
bash
bash
练习
统计/etc/passwd文件中’bash’出现了多少次
[root@lwq network-scripts]# cat /etc/passwd|grep -o 'bash'|wc -l
19
$ 代表以什么结尾 bash$ 以bash结尾的行
^ 代表以什么开头 ^bash 以bash开头的行
[root@master ~]# cat /etc/passwd|grep ^root
root:x:0:0:root:/root:/bin/bash
root123:x:1010:1010::/home/root123:/bin/bash
[root@master ~]# cat /etc/passwd|grep "bash$"
root:x:0:0:root:/root:/bin/bash
dengchao:x:1001:1001::/home/dengchao:/bin/bash
duanyouxu:x:1002:1002::/home/duanyouxu:/bin/bash
liuqingtian:x:1003:1003::/home/liuqingtian:/bin/bash
liwenqian:x:1004:1004::/home/liwenqian:/bin/bash
hejin:x:1005:1005::/home/hejin:/bin/bash
awk
截取字段
awk ‘{[pattern] action}’ {filenames} # 行匹配语句 awk ” 只能用单引号
字段是被空白分隔开的,所以一个空白就是一个分隔符
输出文本中的1、4项 $ awk ‘{print $1,$4}’ log.txt
字段 field --》一段字符串
分隔符 --》分开,隔离的符号 --》默认的分隔符是空白 --》awk
空白: 空格、tab、回车
1 2 3
1 2 3 4 (字段就有:1、2、3、4)
输入分隔符 --》空白
输出分隔符 --》默认是一个空格
指定输出分隔符是,
print 是awk内部的一个输出命令
awk 是一门语言
[root@lier ~]# df -h|grep "/$"
/dev/mapper/centos-root 18G 3.1G 15G 18% /
[root@lier ~]# df -h|grep "/$"|awk '{print $5}'
18%
$5表示截取第5个字段
tr
tr 是一个字符串的处理命令
-d 指定删除的字符串 delete
[root@lier ~]# df -h|grep "/$"
/dev/mapper/centos-root 18G 3.1G 15G 18% /
[root@lier ~]# df -h|grep "/$"|awk '{print $5}'
18%
[root@lier ~]# df -h|grep "/$"|awk '{print $5}'|tr -d "%"
18
()
shell编程中一个圆括号()可以创建一个子进程去执行命令的
==**命令替换 ( ) ∗ ∗ = = : 作 用 优 先 执 行 ()**==: 作用优先执行 ()∗∗==:作用优先执行(命令),然后可以将命令的执行结果赋值给变量
[root@lier ~]# num=$(df -h|grep "/$"|awk '{print $5}'|tr -d "%")
[root@lier ~]# echo $num
18
脚本
[root@lier 426]# cat monitor_root_partination.sh
#!/bin/bash
#得到root分区的使用率
num=$(df -h|grep "/$"|awk '{print $5}'|tr -d %)
if (( $num >= 80 )) ;then
echo "root partition is not enough usage: $num%"
else
echo "root partition is enough usage: ${num}%"
fi
[root@lier 426]# bash monitor_root_partition.sh
root partition is enough! usage: 18%
练习
编写监控脚本,监控/和/boot分区的使用率,/ 大于60%就告警,在屏幕上输出内容,具体自己定义
/boot分区大于50%就告警
脚本名monitor_partition.sh
将磁盘的使用率写到日志文件里到/var/log/root_boot_partition.txt,具体内容自己定义
2022-4-26 22:01:01 /boot used %45
2022-4-26 22:01:01 / used 70%
[root@lier 426]# cat monitor_partition1.sh
#!/bin/bash
num_root=$(df -h|grep "/$"|awk '{print $5}'|tr -d "%")
num_boot=$(df -h|grep "/boot$"|awk '{print $5}'|tr -d "%")
date=$(date +%Y%m%d' '%H:%M:%S)
if ((num_root > 60)) || ((num_boot > 50));then
echo "root partition is not enough!"
echo "/boot_usage: ${num_boot}% /_usage: ${num_root}%"
else
echo "root partition is enough!"
echo "/boot_usage: ${num_boot}% /_usage: ${num_root}%"
fi
echo "$date /boot used ${num_boot}%" >> /var/log/root_boot_partition.txt
echo "$date / used ${num_root}%" >> /var/log/root_boot_partition.txt
[root@lier 426]# bash monitor_partition1.sh
root partition is enough!
/boot_usage: 34% /_usage: 18%
制定计划任务每间隔5分钟执行一下monitor_partition.sh
[root@lier 426]# crontab -l
#30 3 * * * bash /lianxi/tar/backup_log.sh
#30 4 * * 3,5,7 bash /lianxi/backup.sh
*/5 * * * * bash /lianxi/426/monitor_partition1.sh
[root@lier log]# cat root_boot_partition.txt
20220427 09:45:01 /boot used 34%
20220427 09:45:01 / used 18%
20220427 09:50:01 /boot used 34%
20220427 09:50:01 / used 18%