0

I'm trying to compare dates from a file and the current day. The script should run every day and if the dates do not compare it should give a warning.

I made a test file with a 2015 date, but it keeps saying its "equal" to the current date.

#!/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
Hoaxr
  • 73
  • 2
  • 7
  • Using mm-dd-yy dates is never going to work properly. Switch to properly machine (and human!) readable yyyy-mm-dd dates; you will thank yourself many times over. – tripleee Sep 05 '16 at 16:28

3 Answers3

0

You can assign the date output to the today and filedate variables using command substitution. And you'd better double quote your variables in your comparison test:

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
SLePort
  • 15,211
  • 3
  • 34
  • 44
0

find utility is your friend.

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

As a piece of advice you may have to read carefully what atime and ctime means to the system time.

Marco Silva
  • 323
  • 1
  • 6
0

This works for me.

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
Hoaxr
  • 73
  • 2
  • 7