Shell脚本,包含了[分支 循环 数组 搜索 运算等基本功能]

lianshaohua 2016-02-18

脚本内容

#!/bin/sh

currDate=(2016-02-08 2016-02-09 2016-02-10 2016-02-11 2016-02-12 2016-02-13 2016-02-14)
#currDate=(2016-02-08)
#currHour=(00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23)
currHour=(11 12 13 14 15)
currMin=(0 1 2 3 4 5)
currDir=/home/cec/tmp
logDir=/home/cec/ngc/tomcat/logs
machineID=250


port50=5020
port63=3060

sumTmp=0
# copy 需要处理的文件
for var in ${currDate[@]}
do
    echo $var
	if [ ! -f "${currDir}/catalina.out.${var}" ]; then
	    echo ${currDir}/catalina.out.${var}
		#cp ${logDir}/catalina.out.${var}.gz ${currDir}
		#gunzip ${currDir}/catalina.out.${var}.gz
	fi
	
	rm -rf /home/cec/tmp/${var}_ng_version_${port50}.log
	rm -rf /home/cec/tmp/${var}_ng_version_${port63}.log
	#rm -rf /home/cec/tmp/${var}_ng_version2.log
	
	# 在多个文件中查找关键字
	files=$(ls /home/cec/.ngc/logs/ngc.ticket.${var}.*)
	for fileName in ${files[@]}
	do
		echo ${fileName}
		
		#grep -A1 -B2 "ID_DLockSeat\"" ${fileName} >> /home/cec/tmp/${var}_ng_version2.log
		
		grep -A1 -B2 "ID_DLockSeat\"" ${fileName} | grep -A2 -B1 ":${port50}/scts.service" >> /home/cec/tmp/${var}_ng_version_${port50}.log
		grep -A1 -B2 "ID_DLockSeat\"" ${fileName} | grep -A2 -B1 ":${port63}/scts.client" >> /home/cec/tmp/${var}_ng_version_${port63}.log
				
		#tmpNum=`grep "2016-02-08 11:1.*http-bio.*调用" ${fileName} | wc -l`
		#if [ $tmpNum -gt 0 ]; then
		#    echo ${fileName}$tmpNum
		#fi

		#sumTmp=`expr $sumTmp + $tmpNum`
	done
	#echo $sumTmp
	#ls /home/cec/.ngc/logs/ngc.ticket.${var}.* | xargx grep --color -A2 -B1 ":${port50}/scts.service" > /home/cec/tmp/${var}_${port50}.log
	#ls /home/cec/.ngc/logs/ngc.ticket.${var}.* | xargx grep --color -A2 -B1 ":${port63}/scts.service" > /home/cec/tmp/${var}_${port63}.log
done

#提取关于锁座的请求和错误日志
for var in ${currDate[@]}
do
    echo $var
	if [ -f "${currDir}/catalina.out.${var}" ]; then
	    #grep --color -B3 "NgTicketService.lockSeat(" ${currDir}/catalina.out.${var} > ${var}_lockSeat.log
		#grep -A2 "lockSeats\." ${currDir}/catalina.out.${var} > ${var}_all_lockSeats.log
		echo ${currDir}/catalina.out.${var}
	fi
done

lock_file_name=${currDir}/${machineID}_lock_result`date +"%F_%H:%M:%S"`.log
ng_file_name=${currDir}/${machineID}_ng_result`date +"%F_%H:%M:%S"`.log


# 锁座日志查询分析
for var in ${currDate[@]}
do
	for varHour in ${currHour[@]}
    do
		for varMin in ${currMin[@]}
		do
		    echo "$var\ $varHour:${varMin}"
			tmpTime="$var $varHour:${varMin}"
			#tmpErrNum=`grep "$var\ $varHour:${varMin}" ${currDir}/${var}_lockSeat.log | wc -l`
			
			#tmpAllNum=`grep "$var\ $varHour:${varMin}" ${currDir}/${var}_all_lockSeats.log | wc -l`
			#tmp001Num=`grep -A2 "$var\ $varHour:${varMin}" ${currDir}/${var}_all_lockSeats.log | grep "\"001\"" | wc -l`
			#tmp240Num=`grep -A2 "$var\ $varHour:${varMin}" ${currDir}/${var}_all_lockSeats.log | grep "\"240\"" | wc -l`
			
			#tmpAllMYNum=`grep "$var\ $varHour:${varMin}.*00002" ${currDir}/${var}_all_lockSeats.log | wc -l`
			#tmp001MYNum=`grep -A2 "$var\ $varHour:${varMin}.*00002" ${currDir}/${var}_all_lockSeats.log | grep "\"001\"" | wc -l`
			#tmp240MYNum=`grep -A2 "$var\ $varHour:${varMin}.*00002" ${currDir}/${var}_all_lockSeats.log | grep "\"240\"" | wc -l`
			
			tmpAll50=`grep -B2 "ID_DLockSeat\"" ${var}_ng_version_${port50}.log |grep -A3 "$var $varHour:${varMin}.*调用" | grep ":${port50}/scts.service" | wc -l`
			tmpfail50=`grep -B2 "ID_DLockSeat\"" ${var}_ng_version_${port50}.log |grep -A3 "$var $varHour:${varMin}.*调用失败" | grep ":${port50}/scts.service" | wc -l`
tmpSucc50=`grep -B2 -A1 "ID_DLockSeat\"" ${var}_ng_version_${port50}.log|grep -A3 "$var $varHour:${varMin}.*调用成功"|grep -B3 "Status=\"Success\""|grep ":${port50}/scts.service"|wc -l`
			
			tmpAll63=`grep -B2 "ID_DLockSeat\"" ${var}_ng_version_${port63}.log |grep -A3 "$var $varHour:${varMin}.*调用" | grep ":${port63}/scts.client" | wc -l`
			tmpfail63=`grep -B2 "ID_DLockSeat\"" ${var}_ng_version_${port63}.log |grep -A3 "$var $varHour:${varMin}.*调用失败" | grep ":${port63}/scts.client" | wc -l`
tmpSucc63=`grep -B2 -A1 "ID_DLockSeat\"" ${var}_ng_version_${port63}.log |grep -A3 "$var $varHour:${varMin}.*调用成功"|grep -B3 "Status=\"Success\""|grep ":${port63}/scts.client"|wc -l`

			#         grep -B2 -A1 "ID_DLockSeat\"" 2016-02-08_ng_version2.log | grep -A3 "2016-02-08 11:0.*调用成功"| grep -B3 "Status=\"Success\""| grep ":9080/scts.service"  | wc -l
			
			#echo "${tmpTime}  ${tmpErrNum}  ${tmpAllNum}  ${tmp001Num}  ${tmp240Num}  ${tmpAllMYNum}  ${tmp001MYNum}  ${tmp240MYNum}" >> $lock_file_name
			echo "${tmpTime}  ${tmpAll50}  ${tmpfail50}  ${tmpSucc50}  ${tmpAll63}  ${tmpfail63}  ${tmpSucc63}" >> $ng_file_name
		done
	done
done

相关推荐