2016-09-05 33 views
0

我試圖比較文件和當天的日期。 腳本應該每天運行,如果日期不比較,應該給出警告。Bash備份檢查程序

我用2015年的日期做了一個測試文件,但它一直說它與當前日期「相等」。

#!/bin/bash 

today= date +"%m-%d-%y" 
filedate= date +"%m-%d-%y" -r fileName.txt 

if [ $today == $filedate ]; 
then 
echo $today; 
echo $filedate; 
echo 'Backup OK'; 
else 
echo $today; 
echo $filedate; 
echo 'Backup ERROR'; 
fi 
+0

使用mm-dd-yy日期永遠不會正常工作。切換到正確的機器(和人類!)可讀的yyyy-mm-dd日期;你會多次感謝你自己。 – tripleee

回答

0

您可以分配date輸出使用command substitutiontodayfiledate變量。而且你最好在你的對比測試中雙引用你的變量:

today=$(date +"%m-%d-%y") 
filedate=$(date +"%m-%d-%y" -r fileName.txt) 

echo $today 
echo $filedate 
if [ "$today" == "$filedate" ]; 
then 
echo $today; 
echo $filedate; 
echo 'Backup OK'; 
else 
echo $today; 
echo $filedate; 
echo 'Backup ERROR'; 
fi 
+0

謝謝它的作品:) – Hoaxr

0

find實用程序是你的朋友。

modified=$(find fileName.txt -mtime -1) 
if [[ -n $modified ]]; then 
    echo OK 
else 
    echo ERROR 
fi 

作爲一條建議,您可能必須仔細閱讀atime和ctime對系統時間的意義。

0

這適用於我。

today=$(date +"%m-%d-%y") 
filedate=$(date +"%m-%d-%y" -r fileName.txt) 

echo $today 
echo $filedate 
if [ "$today" == "$filedate" ]; 
then 
echo $today; 
echo $filedate; 
echo 'Backup OK'; 
else 
echo $today; 
echo $filedate; 
echo 'Backup ERROR'; 
fi