日常工作中shell处理命令的使用
#!/bin/bash
#批量打包日志文件
LOG=/data/serverlog/shop
DIR=`ls -l |grep Jun|awk '{if($7~/9/)print$0}'|awk '$8 > "17:30" '|awk '$8 < "21:30"'|awk '{print $9}'`
for i in $DIR
do
cd $LOG
tar czvf /home/benet/shop.tar.gz $i
done
查询日志文件常用命令:
sudo netstat -auntpl |grep 11710 \\查询端口11710 PID
sudo lsof -p PID|grep log \\查询当前11710端口日志路径
sudo ls -l |grep 11710 |grep lobby|awk '$7="23"{print $0}'|sort -k8 \\查询11710 lobby 23号日志文件并按时间排序
sudo ls -l |grep 11710 |awk '{if($7~/21/)print $0}'|sort -k8 \\查询11710 21号日志文件并按时间排序
sudo ls -l |grep Jun|awk '{if($7~/9/)print$0}'|awk '$8 > "17:30" '|awk '$8 < "21:30"'|awk '{print $9}' \\查询6月9日17:30至21:30的日志文件
sudo ls -l |grep Jun|awk '{if($7~/9/)print$0}'|awk '$8 > "17:30" '|awk '$8 < "21:30"'|awk '{print $9}'|xargs tar czvf /home/benet/log.tar.gz
日志统计Shell脚本:
#!/bin/bash
cd /data/fslogger
Date=`date +%-d`
#Date=`expr $Date - 1`
TIME=`date +%F-%X`
BAK_DIR=/bak/scripts/log-size.txt
#lobby日志统计
#lobby=`ls -l |grep lobby |grep Aug |sort -k7 |awk '{if($7~/'$Date2'/)print $0}' |awk '{total+=$5}END{print total}'`
lobby2=`ls -lh |grep lobby |grep Aug |sort -k7 |awk '{if($7~/'$Date'/)print $0}' |awk '{total+=$5}END{print total}'`
#SUM1=`expr $lobby / 1024`
#SUMA=`expr $lobby2 + 0`
#AA=`expr $SUMA / 1024`
#dbplayer日志统计
#dbplayer=`ls -l |grep dbplayer |grep Aug |sort -k7 |awk '{if($7~/'$Date2'/)print $0}' |awk '{total+=$5}END{print total}'`
dbplayer2=`ls -l |grep dbplayer |grep Aug |sort -k7 |awk '{if($7~/'$Date'/)print $0}' |awk '{total+=$5}END{print total}'`
#SUM2=`expr $dbplayer / 1024`
SUMB=`expr $dbplayer2 / 1024`
BB=`expr $SUMB / 1024`
echo $TIME >> $BAK_DIR
echo "" >> $BAK_DIR
echo "当前lobby日志大小为:$lobby2 MB" >> $BAK_DIR
echo "" >> $BAK_DIR
echo "当前dbplayer日志大小为:$BB MB" >> $BAK_DIR
echo "" >> $BAK_DIR
echo "========================================================================" >> $BAK_DIR