#!/usr/bin/env bash # Created By Vanch at 2018/9/20
printHelp() { echo "Uncompess log files from inputed zip" echo "Then Merge these logs to one file" echo "Supported file types: zip tar tar.gz tar.bz2" echo echo "Use -s for filtering socket result to socket.log" echo echo "Have fun!" }
#如果没输入参数,就打印帮助信息 if [ $# -eq 0 ]; then printHelp exit 0 fi
#把长选项转到短选项 for arg in "$@"; do shift case "$arg" in "--help") set -- "$@" "-h" ;; "--version") set -- "$@" "-v" ;; "--list") set -- "$@" "-l" ;; *) set -- "$@" "$arg" esac done #获取短选项 OPTIND=1 printS=false; while getopts "dmksahvl" opt; do case $opt in h) #输入为help,就打印帮助信息 printHelp exit 0;; l) #支持单独获取支持文件后缀列表 echo "Supported file types: zip tar tar.gz tar.bz2" exit 0;; v) #支持查找版本号 echo "1.0.0" exit 0;; s) #过滤Socket printS=true;; esac done
#获得压缩包地址 file=${!#} #如果不存在就退出 if [ ! -f "$file" ]; then echo "File not exist!" exit 0; fi #获取压缩后缀 fileName=`basename $file` suffix=${fileName#*.} #判断文件类型 support=('tar','tar.gz','tar.bz2','zip') if [ -z `echo "${support[@]}" | grep -w "$suffix"` ] ; then echo "File type not support!" exit 0; fi #拼接文件夹地址 fileDir=$(dirname $file)/${fileName%%.*} if [ -d $fileDir ]; then rm -rf $fileDir fi mkdir $fileDir cd $fileDir #解压文件 case $suffix in 'tar') eval "tar xvf $file > /dev/null 2>&1";; 'tar.gz') eval "tar zxvf $file > /dev/null 2>&1";; 'tar.bz2') eval "tar jxvf $file > /dev/null 2>&1";; 'zip') eval "unzip -o $file > /dev/null 2>&1";; esac echo 'Uncompass Success!'
#获取日志列表,按排序合并到一个日志 mergeFile=./merge.log logCount=0 #搜索com开头的日志,按日期排序,用?临时代替空格 for logName in `ls | grep 'com' | sort -n | tr " " "?"`; do logName=${logName//'?'/' '} cat ./"$logName" >> $mergeFile ((logCount++)) done #不存在日志就打断 if [ $logCount -eq 0 ]; then echo "Log not exist!" exit fi echo 'Merge Success!' #打印socket if [ $printS = true ]; then cat $mergeFile | grep -i 'socket' >> ./socket.log echo 'Filter socket' fi