首页 > 其他 > 详细

SHELL随笔 日志条目时间比较

时间:2014-03-05 07:45:37      阅读:463      评论:0      收藏:0      [点我收藏+]
  • 背景

    提供DLL函数给客户调用。为了方便调试,每个函数被调用时都会写一条日志。状态函数调用间隔为2s。

    日志格式

    2014:03:04 06:04:06 [Interface.cpp:Function1:100] doing ......

    2014:03:04 06:04:07 [Interface.cpp:Function2:100] doing ......

  • 需求

    检查函数调用间隔时间是否大于2s。

  • 实现

    使用以下shell脚本    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#! /bin/sh                           
                                       
LOG_FILE=/tmp/dll.txt                
tv1=0                                
tv2=0                                
cat $LOG_FILE | while read line      
do                                   
    time_str=`echo $line | cut -c -19`
                                       
    if [ $tv1 -eq 0 ]; then          
        tv1=`date -d "$time_str" +%s`
    fi                              
                                            
    if [ $tv2 -eq 0 ]; then               
        tv2=`date -d "$time_str" +%s`     
    fi              
   
    inter=`expr $tv2 - $tv1`              
    if [ $inter -gt 2 ]; then             
        echo tv1:$tv1 tv2:$tv2 inter:$inter
        echo $tv1 $line                   
    fi                                    
    tv1=$tv2                              
    tv2=0                                 
                                            
done    
  • 备注
    • cut

    echo $line | cut -c -19 

    取一行的前19个字符

    即 echo "2014:03:04 06:04:06 [Interface.cpp:Function1:100] doing" | cut -c -19

    结果为 2014:03:04 06:04:06

    • date

date -d "$time_str" +%s

即 date -d “2014:03:04 06:24:03” +%s,

结果为:1401112983

    

 

    

SHELL随笔 日志条目时间比较,布布扣,bubuko.com

SHELL随笔 日志条目时间比较

原文:http://www.cnblogs.com/ejsurf/p/3580843.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!