#!/bin/bash # Program: # LZSH server log filter # History: # 2014/07/01 Kom First release # 2014/07/03 Kom Remote and multi-param support # Set PATH PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH # Set script name variable SCRIPT=`basename ${BASH_SOURCE[0]}` # 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}的帮助文档"\\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 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 CMDK="" for k in ${KEYS[@]} do CMDK=${CMDK}"| grep $k " done CMDI="" if [ ! -z $IP ]; then CMDK=$CMDK">> $TMPOUTPUT" CMDI="echo > $TMPOUTPUT" else CMDK=$CMDK">> $OUTPUT" CMDI="echo > $OUTPUT" fi for i in ${INPUTS[@]} do CMDI=${CMDI}" && cat $i $CMDK" done if [ ! -z $IP ]; then echo -e \\n"将要执行ssh命令:" echo "${BOLD}ssh $USER@$IP \"$CMDI\"${NORM}" sh -c "ssh $USER@$IP \"$CMDI\"" 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}$CMDI${NORM}" sh -c "$CMDI" fi exit 0
参考:http://tuxtweaks.com/2014/05/bash-getopts/
原文地址:http://blog.csdn.net/kibaamor/article/details/37563463