Search code examples
shellunixunix-timestamp

Unix script to compare a timestamp with current time


This is my first journey into the realm of Unix scripting and I'm not sure how to go about this. Ill be querying a DB and pulling out a timestamp. What I need to do is take that timestamp (in the awesome format of YYYYMMDDHHMMSS) and if its more than 10 minutes old, return a 1 else return 0.

Again, I have essentially 0 experience with this type of scripting (background is in C++ and C#) so if you guys don't mind a little more explanation I'd be grateful - I want to learn how it works too.

Thanks!


Solution

  • Assume $dbtimestamp has the timestamp returned from the database, but I'm hard-coding it here.

    dbtimestamp=20120306142400
    
    secondsDiff=$(( `date '+%Y%m%d%H%M%S'` - $dbtimestamp ))
    if [ $secondsDiff -gt 600 ] 
    then
      exit 1
    else
      exit 0
    fi