我需要比较日志文件中最后两行的时间戳,以验证日志是否是最新的



我有一个日志文件,它每60秒更新一次,每更新一行

持有以下格式:

IP - - [22/Nov/2013:16:51:38 +0000] "GET /some path" 500 305
IP - - [22/Nov/2013:16:52:28 +0000] "GET /some path" 500 305

这是我迄今为止拥有的代码:

#! /bin/bash
#This gets the time stamp of the last log
file1=`ssh root@IP  tail -n 1  path/file | cut -d'/' -f3 | awk '{print $1}'` 
sleep 60
#This allows the script to sleep for 60 sec while the logs populate
file2=`ssh root@IP tail -n 1  path/file | cut -d'/' -f3 | awk '{print $1}'`
#This is the 2nd time that the file log is tested

if [ $file1 -eq $file2 ]; then  
#comparing 2 time stamp variables
echo "The Access Logs are NOT current!" 
fi

代码没有正确地比较时间戳,它总是会回显

访问日志不是最新的,即使它们是最新的。我可能认为有一个转换

必须以Unix格式进行,但不确定如何进行。

问题可能是这个

[ $file1 -eq $file2 ]

file1file2包含诸如2013:16:51:38之类的字符串,并且您正在对此执行数值相等性检查。

请改用[ "$file1" = "$file2" ]。这会进行字符串比较。

最新更新