美文网首页
一个查看日志的脚本

一个查看日志的脚本

作者: 疲马羁禽 | 来源:发表于2016-09-30 02:57 被阅读94次

一个查日志的脚本,用处不大,还需要完善

#!/bin/bash
#Write by clb@tzg.cn
#Date:2016-9-29
####################定义功能函数部分#####################
function print_error {
        printf "\033[32;1m使用方式:  $0 -a -b -c
        -a:根据关键字打印日志
        -b:根据时间打印日志
        -c:打印实时日志
        \033[0m\n"
        exit 1
}

function print_date {
        printf "\033[1;32m 输入日期 例:09 06\033[0m\n"
        read  m d
        if [ $m -gt 0 -a $m -lt 13 -a $d -gt 0 -a $d -lt 32 ];then
        printf "\033[1;32m 2016年$m月$d日\033[0m\n"
        else
        printf "\033[1;32m 请输入正确的日期\033[0m\n"
        exit 1
        fi
}

function print_apply {
        printf "\033[1;32m 输入需要查询的应用\033[0m\033[1;44;31;5m\n   actprovider   act   app   awardsgrant   bi\n   b   callback   credit   currentbao   fixedbao\n   growthpoint   mq   pay   pyramid   rest   rmi\n   sms   task   wap   web\033[0m\n"
        read  apply
        if [[ $applys =~ $apply ]] ;then
        printf "\033[1;32m 寻找$apply的日志\033[0m\n"
        else
        printf "\033[1;32m 请输入正确应用名\033[0m\n"
        exit 1
    fi
}

function print_key {
        printf "\033[1;32m 输入关键字\033[0m\n"
        read keyword
        cd /opt/logs/2016.$m.$d
        cat $apply.tomcat-catalina-out | grep -n $keyword
}

function print_time {
        cd /opt/logs/2016.$m.$d
        printf "\033[1;32m 输入时间 例:14 05\033[0m\n"
        read  H M
        if [ $H -ge 0 -a $H -le 23 -a $M -ge 0 -a $M -le 59 ];then
        printf "\033[1;32m $H时$M分\033[0m\n"
        else
        printf "\033[1;32m 请输入正确的时间\033[0m\n"
        exit 1
    fi
        nu1=$(cat $apply.tomcat-catalina-out | grep -n $H:$M: | awk 'NR==1{print}' | awk -F ":" '{print $1}')
        nu2=$[$nu1+500]
        cat $apply.tomcat-catalina-out | awk 'NR>nu1&&NR<nu2' nu1="$nu1" nu2="$nu2"
}

function print_tail {
        m=$(date -d today +"%m")
        d=$(date -d today +"%d")
        cd /opt/logs/2016.$m.$d
        tail -200f $apply.tomcat-catalina-out
}

##############脚本执行部分#################
applys="actprovider act app awardsgrant bi b callback credit currentbao fixedbao growthpoint mq pay pyramid rest rmi sms task wap web"

[ -z "$1" ] && print_error
while getopts abc opt;do
        case "$opt" in
                a) a=true;;
                b) b=true;;
                c) c=true;;
                *) print_error;;
        esac
done
[ -z "$a$b$c" ] && print_error
[ "$a" = true ] && print_date && print_apply && print_key
[ "$b" = true ] && print_date && print_apply && print_time
[ "$c" = true ] && print_apply && print_tail

相关文章

网友评论

      本文标题:一个查看日志的脚本

      本文链接:https://www.haomeiwen.com/subject/uuulyttx.html