一个查看日志的脚本

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

#!/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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 寻找一种易于理解的一致性算法(扩展版) 摘要 Raft 是一种为了管理复制日志的一致性算法。它提供了和 Paxos...
    枝叶君阅读 7,595评论 0 15
  • 养猫一时,猫奴一世 侍奉好喵主子是猫奴的首要任务之一 #寻觅视频# 我们办公室就有一位喵主子 简称瞌总大人 此猫仗...
    寻觅视频阅读 2,523评论 0 0
  • 若有一天,你误打误撞来到这里 一定会觉得,这座被遗忘的山峰,是神赋予的礼物 它洗去你俗世的尘埃,荡涤你的每一个毛孔...
    IAMME_09b1阅读 1,722评论 0 2
  • 大家一想到地中海风格的家居,一般是不是会想到这几种设计元素:白灰泥墙、拱廊或拱门,海蓝色的门窗。地中海风格的灵魂,...
    1fa8a0d17577阅读 3,630评论 0 1