shell脚本基础(awk扩展)

例1:监控磁盘使用率,高于80%的找出来,输出警告,发送邮件给管理员

$ df -h |grep -v Filesystem |awk '{print $1,$5}'|awk -F% '{print $1}'| awk '$2>80 {print $1"useage is over 80%,warning!!!"}' |mail -s "disk overuse problem" root 
$ df -h |  awk 'NR>1 {print $4"\t"$5}'  | grep %  | grep -v G | grep -v M | awk -F'%' '$1>80 {print $1"useage is over 80%,warning!!!"}' | mail -s "warring" root@localhost
$ df -k |grep -v Filesystem |awk '$3/$2>0.8 {print $1"useage is over 80%,warning!!!"}' |mail -s "disk overuse problem" root 

例2:监控CPU 15分钟内的平均负载,如果超过5则发警告邮件给管理员

uptime  |awk '$NF>5 {print "high load,warning"}' |mail -s 'xxxxx' root

例3:计算swap总量,使用量,剩余量,使用百分比,剩余百分比

格式要求为:
total used free used% free%
xxxxx xxxx xxxx xxx% xxx%

free |tail -1 |awk 'BEGIN {print "total\t\tused\tfree\tused%\tfree%"} {print $2"\t\t"$3"\t"$4"\t"$3/$2*100"%\t"$4/$2*100"%"}'
total           used    free    used%   free%
4096564         0       4096564 0%      100%

利用printf格式化输出

$ awk 'BEGIN {print "hello world"}'
hello world
$ awk 'BEGIN {printf "hello world"}'
hello world# 

–从上面看到print和printf直接使用的主要区别就是printf不自动换行,需要使用\n来换行

$ awk 'BEGIN {printf "Hello world\n" }'
Hello world

格式:
printf {format_expression[,argument]}
两个主要的格式说明符是s和d,s代表字符串,d表示十进制整数

$ ls -l | grep -v total | awk '{printf ("%s的大小为%d\n",$NF,$5)}' |tail 
Templates的大小为4096
test的大小为25
test1的大小为9
test.sh的大小为258
test*?[].sh的大小为0
user_passwd.txt的大小为60
Videos的大小为4096
while_useradd_shift.sh的大小为100
who.out的大小为320
内部变量的大小为76
$ awk -F: 'BEGIN {sum=0}  $NF=="/sbin/nologin" {printf ("%s\t",$1); sum=sum+1}  END {print "\n\n总共有:"sum"个"}' /etc/passwd 
bin     daemon  adm     lp      mail    uucp    operator        games gopher   ftp     nobody  vcsa    rpc     mailnull        smmsp   nscd  ntp      oprofile        pcap    dbus    avahi   xfs     named   rpcusernfsnobody       sshd    haldaemon       gdm     avahi-autoipd   sabayonapache  squid   ais     mfs     postfix distcache       cacti

总共有:37个

1,利用awk把不允许登录的账号名打印出来(指第七列不是以/bin/bash结束的列),并统计个数

$ awk -F: 'BEGIN {sum=0} $NF!~"/bin/bash" {printf ("%s ",$1);sum=sum+1} END {print "\n\n总共的个数 为:"sum}' /etc/passwd

2,把系统上ssh的登录日志格式化打印成类似下面的


time hostname user ip port protocol
Jul-15 15:47:20 li root 2.2.2.35 6703 ssh2



time hostname user ip port protocol
Jul-15 15:47:27 li root 2.2.2.35 6704 ssh2


$ cat /var/log/secure |grep Accepted |awk '{print "***************************************************************************"}{print "time\t\thostname\tuser\tip\t\tport\tprotocol"}{print $1"-"$2,$3"\t"$4"\t\t"$9"\t"$11"\t"$13"\t"$14}{print "*************************************************************************"}'
$ cat /var/log/secure |grep Accepted |awk ' BEGIN{print "time\t\thostname\tuser\tip\t\tport\tprotocol"}{print $1"-"$2,$3"\t"$4"\t\t"$9"\t"$11"\t"$13"\t"$14}'
  • 11
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值