刚接触shell 第一个处女小小程序

本文介绍了一个用于分析特定文件的日志处理Shell脚本。该脚本能够筛选并统计特定字符串出现的次数,比较不同文件的行数,并检查规则文件中是否存在某些条目。此外,还涉及了如何处理重复触发的用户规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/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列 将没匹配到的行打印出来
echo "start analsys $file NO Match str is :"
cat -n $file | grep -v 'Match str:'

# 统计访问网站和搜索触发的行数
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"
# 对比相同文件名的trigger和trigger_log行数是否相等
name1=${file%log*.dat} #从尾部开始将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
#若同一个用户出现两次以上检测 规则是否在rule文件中
user=`cat $file | cut -d ',' -f 1 |uniq -c |awk '$1 > 2 {print $2}'` #得出两次或两次以上触发的用户有哪些

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里根据match str后面的内容 在规则文件里查找是否有匹配的
while read line
do
fname=${line%%,*} #获取最终rule文件名
ctent=${line##* }
content=${ctent%%,*} #获取最终要匹配的内容
if [ ! `cat -n ${fname}.rule|grep "$content"` ];then
echo "$content is not in ${fname}.rule"
fi
done < rule
fi
done

很山寨的程序,这可是我的第一个shell啊。。明天经理要看,,希望他看后的狂虐可以让我提高一大截。

转载于:https://www.cnblogs.com/tears-of-ramos/archive/2013/06/07/3124317.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值