bashshell

How to convert date to timestamp in bash shell?


I need to write a script to check one of my log files to check its last updated time. If it's greater than 90 minutes, I need to get alerted. But now I am stuck in timestamp conversion.

I have the below line in my code, but it's not working. It's throwing an error like ./monitor_log.sh: line 13: Aug: command not found.

modifeidTimestamp=$(date --date="`$lastModified`" +"%s");

The command below is working fine, but I have to use a variable. Can anyone help?

 date --date='Aug 25 06:07:30' +"%s"

Solution

  • You can do:

    lastModified='Aug 25 06:07:30'
    modifeidTimestamp=$(date --date="$lastModified" +"%s")
    
    echo "$modifeidTimestamp"
    1440497250
    

    No need to use reverse tick around lastModified variable. Just wrap it in double quotes.