简体   繁体   中英

Bash comparing two timestamps

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

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 ... if [ $endts -lt $(($(date +%s) - 86400 )) ] ; then ...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM