Bash - date calculation

Posted on Fri 01 January 2010 by Pavlo Khmel

In log file /var/log/khmel.org.log can be message like:

16.10.2010 00:05:59 KHMEL.ORG site find AASSxxRR0111 connection

To convert date and time to unix format, and calculate "does it happend last 24 hours":

if [ -f /var/log/khmel.org.log ]; then
  LastMessageHuman="`grep "KHMEL.ORG site find .* connection" /var/log/khmel.org.log | tail -n 1 | awk '{print $1"."$2}' | awk -F . '{print $3"-"$2"-"$1" "$4}'`";
  LastMessageUnix="`date -d "$LastMessageHuman" +%s`";
  TimeNowUnix="`date +%s`";
  let TimeBack24Unix=$TimeNowUnix-86400;
    if [ "$TimeBack24Unix" -lt "$LastMessageUnix" ]; then
      echo "Less then 24 hours ago";
    else
      echo "MOre then 24 hours ago";
    fi;
fi;