剛接觸shell 第一個(gè)處女小小程序
#!/bin/bash
# HJ test trigger file
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
directory=/home/houjiao/test/
filelist=`ls $directory`
#for file in $filelist
for file in *.dat
do
exist=$(lsof ${directory}${file})
if [ $? ] && [ `echo $file |grep 'log'` ]; then
# 是否匹配到Match str列 將沒(méi)匹配到的行打印出來(lái)
echo "start analsys $file NO Match str is :"
cat -n $file | grep -v 'Match str:'
# 統(tǒng)計(jì)訪問(wèn)網(wǎng)站和搜索觸發(fā)的行數(shù)
cat -n $file | grep '\.c[on]m*' > count.site
netrow=`cat count.site |wc -l`
allrow=`cat $file |wc -l`
seachrow=$(($allrow - $netrow))
echo "According to the website's count $netrow"
echo "According to the search's count $seachrow"
# 對(duì)比相同文件名的trigger和trigger_log行數(shù)是否相等
name1=${file%log*.dat} #從尾部開(kāi)始將log刪除
name2=`echo $file |cut -d '_' -f 4`
filename=${name1}${name2} #去掉log后的文件名
if [ -e ${directory}${filename} ]; then
trigger=`cat $filename |wc -l`
test $trigger -eq $allrow && echo "$filename equal with $file" || echo "$filename not equal with $file"
fi
#若同一個(gè)用戶出現(xiàn)兩次以上檢測(cè) 規(guī)則是否在rule文件中
user=`cat $file | cut -d ',' -f 1 |uniq -c |awk '$1 > 2 {print $2}'` #得出兩次或兩次以上觸發(fā)的用戶有哪些
for userid in $user
do
#將match rule:id 和match str:www.taobao.com這兩列存入rule.test
cat $file |grep "^$userid"|cut -d ':' -f 2,3 >> rule
done
# 在rule.test里根據(jù)match str后面的內(nèi)容 在規(guī)則文件里查找是否有匹配的
while read line
do
fname=${line%%,*} #獲取最終rule文件名
ctent=${line##* }
content=${ctent%%,*} #獲取最終要匹配的內(nèi)容
if [ ! `cat -n ${fname}.rule|grep "$content"` ];then
echo "$content is not in ${fname}.rule"
fi
done < rule
fi
done
很山寨的程序,這可是我的第一個(gè)shell啊。。明天經(jīng)理要看,,希望他看后的狂虐可以讓我提高一大截。

浙公網(wǎng)安備 33010602011771號(hào)