#!/bin/bash
# Program:
# LZSH server log filter
# History:
# 2014/07/01 Kom First release
# 2014/07/03 Kom Remote and multi-param support
# 2014/07/18 Kom Fix multi keyword bug
# 2014/07/18 Kom Continue exe even if some cmd failed
# 2014/07/18 Kom optimize
# Set PATH
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#Disable filename globbing
set -f
# Set script name variable
SCRIPT=`basename ${BASH_SOURCE[0]}`
SCRIPT_VER=20140718-1809
# Initialize variables to default values
IP=
USER=
TMPOUTPUT=
declare -a KEYS
declare -a INPUTS
OUTPUT=
# Set fonts for help
NORM=`tput sgr0`
BOLD=`tput bold`
REV=`tput smso`
# Help function
function HELP
{
echo -e \n"${BOLD}龙之守护数据过滤脚本${SCRIPT}${NORM}的帮助文档"
echo -e "${BOLD}版本:${SCRIPT_VER}${NORM}"\n
echo -e "${REV}用法:${NORM} ${BOLD}$SCRIPT 参数${NORM}"\n
echo "可选参数:"
echo "${REV}-a${NORM} --输入文件所在的IP,为空表示本地(默认为空)"
echo "${REV}-u${NORM} --访问输入文件所用的用户名,当文件在本地时不使用此参数"
echo "${REV}-k${NORM} --用于过滤的关键字,此参数可以多个联合使用(例如:-k物品创建 -k强化石)"
echo "${REV}-i${NORM} --输入文件的全路径,此参数可以多个联合使用(例如:-i/home/log/objscenesserver21.log.140701-* -i/home/log/objscenesserver40.log.140701-*)"
echo "${REV}-o${NORM} --本地输出文件的全路径(例如:-o/local/lzsh.log)"
echo "${REV}-t${NORM} --临时输出文件的全路径,当文件在本地时不使用此参数"
echo -e "${REV}-h${NORM} --显示此帮助并退出"\n
echo -e "示例: "
echo -e "${BOLD}$SCRIPT -a127.0.0.1 -uUsername -k物品创建 -k强化石 -i/home/log/objscenesserver21.log.140701-* -o/local/lzsh.log -t/tmp/lzsh.log${NORM}"\n
exit 1
}
# Check the number of argument. If none are passed, print help and exit.
if [ $# -eq 0 ]; then
HELP
else
echo "输入参数:${BOLD}$*${NORM}"
fi
while getopts :a:u:t:i:o:k:h FLAG; do
case $FLAG in
a)
IP=$OPTARG
;;
u)
USER=$OPTARG
;;
t)
TMPOUTPUT=$OPTARG
;;
i)
INPUTS=(${INPUTS[@]} $OPTARG)
;;
o)
OUTPUT=$OPTARG
;;
k)
KEYS=(${KEYS[@]} $OPTARG)
;;
h) # show help
HELP
;;
?) # unrecognized option - show help
echo -e \n"非法参数:-${BOLD}$OPTARG${NORM}"
HELP
;;
esac
done
shift $((OPTIND-1)) # This tells getopts to move on to the next argument
#######################################################################
# Parameters check
if [ ${#KEYS} -eq 0 ]; then
echo "你必须指定至少一个${BOLD}关键字${NORM}"
exit -1
else
echo "关键字:${BOLD}${KEYS[@]}${NORM}"
fi
if [ ${#INPUTS} -eq 0 ]; then
echo "你必须指定至少一个${BOLD}输入文件${NORM}"
exit -1
else
echo "输入文件:${BOLD}${INPUTS[@]}${NORM}"
fi
if [ -z $OUTPUT ]; then
echo "你必须指定${BOLD}本地输出文件${NORM}"
exit -1
fi
echo "输出文件:${BOLD}${OUTPUT[@]}${NORM}"
if [ ! -z $IP ]; then
if [ -z $USER ]; then
echo "必须指定用于访问${BOLD}${IP}${NORM}的${BOLD}用户名${NORM}"
exit -1
fi
if [ -z $TMPOUTPUT ]; then
echo "必须指定在${BOLD}${IP}${NORM}上的${BOLD}临时输出文件${NORM}"
exit -1
fi
fi
#######################################################################
# Work
TARGETOUTPUT=""
if [ ! -z $IP ]; then
TARGETOUTPUT=$TMPOUTPUT
else
TARGETOUTPUT=$OUTPUT
fi
CMD=""
for k in ${KEYS[@]}
do
CMD=${CMD}"grep $k ${INPUTS[@]} >> $TARGETOUTPUT; "
done
CMD="echo > $TARGETOUTPUT; $CMD"
echo $CMD
if [ ! -z $IP ]; then
echo -e \n"将要执行ssh命令:"
echo "${BOLD}ssh $USER@$IP "$CMD"${NORM}"
sh -c "ssh $USER@$IP "$CMD""
echo -e \n"将要执行scp命令:"
echo "${BOLD}scp $USER@$IP:$TMPOUTPUT $OUTPUT${NORM}"
sh -c "scp $USER@$IP:$TMPOUTPUT $OUTPUT"
else
echo -e \n"将要执行命令:"
echo "${BOLD}$CMD${NORM}"
sh -c "$CMD"
fi
exit 0
上一篇
Linux下Bash中case用法详解
2015-04-22
下一篇
Win32程序发生异常时处理的流程
2015-04-22