好久没有学习shell,为分析日志写了一小段记录如下:
#!/bin/sh
##
# useage: ./an.sh logName
#
##
if [ $# -eq 0 ]; then
echo "useage: ./an.sh logName";
exit 0;
fi
logName=$1
grep getAppTilesDetail ${logName} |grep "API.RQ" |awk -F'[' '{print$2}'|awk -F'-' '{print$5}'|sed 's/\]//g'|sort|uniq
> threadNo.txt
nuArr=`cat threadNo.txt`
threadNamePre="http-nio-8080-exec-"
for var in ${nuArr}
do
threadName=${threadNamePre}${var}
grepStr="\["$threadName"\]"
grep ${grepStr} ${logName} > ./threadfile/$threadName
done
使用到了参数传递、grep、awk、sed、for循环等, 实现了将日志文件中相同线程日志写入一个文件的功能
再来一段:
#!/bin/sh
parseTime() {
OLD_IFS="$IFS"
IFS=","
arr=($1)
IFS="$OLD_IFS"
msStr=${arr[1]}
tStr=${arr[0]}
#echo $msStr
OLD_IFS="$IFS"
IFS=":"
timeArr=($tStr)
IFS="$OLD_IFS"
h=${timeArr[0]}
m=${timeArr[1]}
s=${timeArr[2]}
ret=`expr $h \* 60` # *60+$s))
ret=`expr $ret + $m`
ret=`expr $ret \* 60`
ret=`expr $ret + $s`
ret=`expr $ret \* 1000`
ret=`expr $ret + $msStr`
echo $ret
#return ret
}
rqTime=0
file=$1
cat $file | while read line
#for line in $logarr
do
# echo $line
OLD_IFS="$IFS"
IFS=" "
array=($line)
IFS="$OLD_IFS"
timeStr=${array[1]}
echo $timeStr
if [[ $line =~ "API.RQ:" ]];
then
rqTime=`parseTime $timeStr`
#rqTime=$?
echo $rqTime
#echo $line
echo "now rqTime=" $rqTime
elif [[ $line =~ "API.RP:" ]];
then
rpTime=`parseTime $timeStr`
#rpTime=$?
echo "now rpTime=" $rpTime
takeTime=`expr $rpTime - $rqTime`
echo "takeTime=" $takeTime
if [ $takeTime -gt $2 ]; then
echo "Take time:" ${takeTime}
echo "rp:" $line
fi
rqTime=0
fi
done
使用到了shell的数值计算,逻辑判断等, 为计算接口从请求打印日志到返回打印日志之前的用时 。