day 22
例题52
[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# fg vim 52.sh #将用户家目录下面小于5KB的文件打包成tar.gz的压缩包,并以当前日期为文件名前缀 #!/bin/bash t=`date +%F` cd $HOME tar czf $t.tar.gz `find ./ -type f -size -5k|xargs`
例题53
[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# fg vim 53.sh #监控iptables规则是否封掉了22端口,如果封掉了,给打开。 写好脚本,放到任务计划里,每分钟执行一次 #!/bin/bash iptables -nvL INPUT --line-numbers |grep -w 'dpt:22' |awk '$4 ~/REJECT|DROP/ {print $1}' > /tmp/iptables.log n=`wc -l /tmp/iptables.log` if [ $n -gt 0 ] then for n in `tac /tmp/iptables.log` do iptables -D INPUT $n done fi
例题54
[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# vim 54.sh #已知nginx访问的日志文件在/usr/local/nginx/logs/access.log内,请统计下早上10点到12点 来访ip最多的 #!/bin/bash export LANG=en log="/usr/local/nginx/logs/access.log" t=`date +%d/%b/%Y:1[01]:[0-5][0-9]:` egrep "$t" $log|awk '{print $1}' |sort -n |uniq -c |sort -n |tail -1 |awk '{print $2}'
例题55
[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# vim 55.sh #提示输入一个暂停的数字,然后从1打印到该数字。然后询问是否继续。继续的话再输入一个数字接着打印,否则退出脚本 #!/bin/bash read -p "Please input a number: " n n1=`echo $n |sed 's/[0-9]//g'` if [ -n "$n1" ] then echo "Please input a number." exit fi for i in `seq 1 $n` do echo $i done read -p "If continue? y/n" c case $c in n|N) exit ;; y|Y) read -p "Please input a number: " n2 n3=`echo $n2|sed 's/[0-9]//g'` if [ -n "$n3" ] then echo "Please input a number." exit fi if [ $n2 -le $n ] then echo "$n2 should grater than $n." exit fi for i in `seq $[$n+1] $n2` do echo $i done ;; *) echo "Please input y or n." ;; esac