Compare the system date with date from a text file

I get the date that's inside a text file and assigned it to a variable. When I grep the date from the file, I get this,

Not After : Jul 28 14:09:57 2017 GMT

So I only crop out the date, with this command

echo $dateFile | cut -d ':' -f 2,4

The result would be

Jul 28 14:57 2017 GMT

How do I convert this date to the number of seconds, so I can compare it to the system date? If it is over 2 days old.
I have this code but it doesn't work. I'm getting an error message when I ran it. I think its because $dateFile is a text file and it doesn't know how to convert it.
Any help would be appreciated.

 
 #!/bin/bash
 $dateFile=grep "After :" myfile.txt | cut -d ':' -f 2,4
 AGE_OF_MONTH="172800" # 172800 seconds  = 2 Days
NOW=$( date +%s )
NEW_DATE=$(( NOW - AGE_OF_MONTH ))
 if [ $( stat -c %Y "$dateFile" ) -gt ${NEW_DATE} ]; then
   echo Date Less then 2 days
else
   echo Date Greater then 2 days
fi

 

Hi,

(Edit: I'd initially typo'd the "2 days" as "2 months" in the output routine. Fixed now, sorry.)

I think I have a solution for you. The script is:

#!/bin/bash
file_string=`/bin/cat date.txt | /usr/bin/awk '{print $5,$4,$7,$6,$8}'`
file_date=`/bin/date -d "$file_string"`
file_epoch=`/bin/date -d "$file_string" +%s`
now_epoch=`/bin/date +%s`

if [ "$file_epoch" -gt "$now_epoch" ]
then
        #let difference=$file_epoch-$now_epoch
        difference=`/usr/bin/expr $file_epoch - $now_epoch`
elif [ "$now_epoch" -gt "$file_epoch" ]
then
        #let difference=$now_epoch-$file_epoch
        difference=`/usr/bin/expr $now_epoch - $file_epoch`
else
        let difference=0
fi

if [ "$difference" -ge "172800" ]
then
        echo "More than 2 days between $file_date and now"
else
        echo "Less than 2 days between $file_date and now"
fi

You'll notice that above the 'eval' lines (a command I'm using to do the arithmetic here) there are also commented-out lines using Bash's own built-in 'let' command, which can also do arithmetic. If you don't have 'expr' on your system, then you can comment out or remove the 'expr' lines and go with the 'let' lines instead. I've tested it with both, and (for this one single test input file, it must be noted) all was well.

Here is a transcript of a sample session, using the exact test date string you provided as the input in 'date.txt'.

$ cat date.txt
Not After : Jul 28 14:09:57 2017 GMT
$ ./script.sh
More than 2 days between Fri 28 Jul 15:09:57 BST 2017 and now
$

Hope this helps.

1 Like

Edit: I just noticed you have cut the minutes value out of the original date - I'm assuming this was a mistake and your cut command should probably have been cut -d ':' -f 2-4

If your system has GNU date you can use date -d STRING to display time described by STRING eg:

$ date -d "Jul 28 14:09:57 2017 GMT" +%s
1501250997

If you don't have GNU date you can use a perl program to achieve the same thing (most systems seem to have perl available).

save this a epoch.pl and chmod +x epoch.pl :

#!/bin/perl
use POSIX;
use Time::Local;

my %mon;
@mon{qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/} = 0..11;

my ($mth, $d, $h, $m, $s, $y) = split(/[: ]/, $ARGV[0]);

eval {
  printf "%d\n", timegm(0, $m, $h,$d,$mon{$mth}, $y)
};
printf "ERROR" if $@;

The you can call it like this

$ ./epoch.pl "Jul 28 14:09:57 2017 GMT"
1501250997

@drysdalk

Your solution is functional but can I make a few suggestions to help improve it.

file_string=`/bin/cat date.txt | /usr/bin/awk '{print $5,$4,$7,$6,$8}'`
  1. Awk can open an read files without the help of cat
  2. No need to change order of the fields GNU date would accept a date in the format anyway.
  3. OP requires matching on line containing "After :"

Perhaps: file_string=$(awk -F": " '/After :/ {print $2}')

if [ "$file_epoch" -gt "$now_epoch" ]
then
        #let difference=$file_epoch-$now_epoch
        difference=`/usr/bin/expr $file_epoch - $now_epoch`
elif [ "$now_epoch" -gt "$file_epoch" ]
then
        #let difference=$now_epoch-$file_epoch
        difference=`/usr/bin/expr $now_epoch - $file_epoch`
else
        let difference=0
fi

OP is only wanting to match a filedate older than 2 days, the code above would also match filedate up to 2 days in the future.

OP was already using Arithmetic Expansion to calculate differences between epoch times.
I believe $((expression)) is superior to let as its more portable and is much better than using /usr/bin/expr as this wastes resources starting a new process and loading the expr command binary into memory to execute it.

I'd simply go with:

if (( file_epoch < now_epoch - 2*24*60*60 ))
then
 echo Date Less then 2 days
else
 echo Date Greater then 2 days
fi
2 Likes