本文共 2241 字,大约阅读时间需要 7 分钟。
练习21
计算文档a.txt中每一行中出现的数字个数并且要计算一下整个文档中一共出现了几个数字。例如a.txt内容如下:12aa*lkjskdjalskdflkskdjflkjj我们脚本名字为 ncount.sh, 运行它时:bash ncount.sh a.txt输出结果应该为:20sum:2#!/bin/bash
sum=0while read linedoline_n=echo $line|sed 's/[^0-9]//g'|wc -L
echo $line_nsum=$[$sum+$line_n]done < $1echo "sum:$sum" 练习22
有两台Linux服务器A和B,假如A可以直接ssh到B,不用输入密码。A和B都有一个目录叫做/data/web/ 这下面有很多文件,当然我们不知道具体有几层子目录,假若之前A和B上该目录下的文件都是一模一样的。
但现在不确定是否一致了。固需要我们写一个脚本实现这样的功能,检测A机器和B机器/data/web/目录下文件的异同,我们以A机器上的文件作为标准。
比如,假若B机器少了一个a.txt文件,那我们应该能够检测出来,或者B机器上的b.txt文件有过改动,我们也应该能够检测出来(B机器上多了文件不用考虑)。
#!/bin/bash
dir=/data/web[ -f /tmp/md5.list ] && rm -f /tmp/md5.listfind $dir/ -type f > /tmp/file.listwhile read line domd5sum $line >> /tmp/md5.listdone < /tmp/file.listscp /tmp/md5.list B:/tmp/
[ -f /tmp/check_md5.sh ] && rm -f /tmp/check_md5.shcat >/tmp/check_md5.sh << EOF
#!/bin/bashdir=/data/webn=`wc -l /tmp/md5.list|awk '{print \$1}'`for i in `seq 1 \$n`dofile_name=`sed -n "\$i"p /tmp/md5.list |awk '{print \$1}'`md5=`sed -n "\$i"p /tmp/md5.list|awk '{print \$2}'`if [ -f \$file_name ]thenmd5_b=`md5sum \$file_name`if [\$md5_b != \$md5 ]thenecho "\$file_name changed."fielseecho "\$file_name lose."fidoneEOFscp /tmp/check_md5.sh B:/tmp/ssh B "/bin/bash /tmp/check_md5.sh"练习23
写一个脚本,检测你的网络流量,并记录到一个日志里。需要按照如下格式,并且一分钟统计一次(只需要统计外网网卡,假设网卡名字为eth0): 2017-08-04 01:11eth0 input: 1000bpseth0 output : 200000bps2017-08-04 01:12
eth0 input: 1000bpseth0 output : 200000bps#!/bin/bash
logdir=/tmp/sar_logfile=$logdir/date +%d%H
.logt=date +"%F %H:%M"
[ -d $logdir ] || mkdir -p $logdir
LANG=ensar -n DEV 1 5 |grep eth0 |grep "Average" > /tmp/sar.tmpexec >>$file
echo "$t" awk '{print "eth0 input:",$58000"bps""\n""eth0 output:",$68000"bps"}' /tmp/sar.tmp echo "#### ###################"练习24
#!/bin/bashfor pid inps aux |grep clearnen.sh |awk '{print $2}'
do echo $pidkill -9 $piddone 练习25
写一个脚本判断你的Linux服务器里是否开启web服务?(监听80端口)如果开启了,请判断出跑的是什么服务,是httpd呢还是nginx又或者是其他的什么?#!/bin/bash
n=netstat -lntp |grep ':80 '|wc -l
if [ $n -eq 0 ]thenecho "It not listen port 80"elseser=netstat -lntp |grep ':80 '|awk -F '/' '{print $NF}'|sed 's/ //g'
echo "It is listenning port 80, and the service is $ser."fi 转载于:https://blog.51cto.com/12898947/2340019