Home > Enterprise >  Bash comparing two timestamps
Bash comparing two timestamps

Time:07-25

i want to compare if "endts (it contains a timestamp)" is older than 24hours but im stucked with it.

endline=`cat /xy.json 2>&1 | grep -ie endTime`
endts=`sed s/\"endTime\":\ //g <(echo $endline) | sed s/,//`
current=`date  %s`

if [ $endts -gt $current ]; then
                msg='older than 24hrs'
                exitcd=2
else
                msg='not older than 24hrs'
                exitcd=0
fi

echo $msg
exit $exitcd

i've tried with -gt 86400 but its not working

CodePudding user response:

24 hour is 86400 seconds. So in order to compare if a date ( seconds) is older than 24 hrs, you have to compare it with date %s - 86400

And the comparison should be -lt ( less than

... 
if [ $endts -lt $(($(date  %s) - 86400 )) ] ; then 
... 

If true $endts was generated more than 24 hours ago.

  • Related