2013-11-22 28 views
1

我有更新每60秒一個日誌文件,並每個新行我需要最後2個時間戳在日誌文件進行比較,以驗證日誌是當前

持有以下格式:

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 [email protected] 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 [email protected] 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格式,但不知道如何完成。

回答

0

的問題可能是這個

[ $file1 -eq $file2 ]

file1file2包含字符串,如2013:16:51:38和你正在運行的這個數值相等檢查。

改爲使用[ "$file1" = "$file2" ]。這是一個字符串比較。

+0

我已經試過雙引號之前,我得到了同樣的錯誤。 – user2876750

+0

@ user2876750,它不只是雙引號,而是從'eq'切換到'=' – iruvar

+0

Worked!謝謝! – user2876750

相關問題