Skip to main content
2 of 4
Thanks is not required
AReddy
  • 3.2k
  • 6
  • 37
  • 77

How to compare two dates in script to check expiration

I am creating a script where I can check to see if something is expired such as a kerberos ticket cache.

I need to get the expiration date/time, get the current date/time, and compare them to see if this first date/time would be expired.

I am creating a script to see if a kerberos ticket is still valid, or expired.

I can get the expiration date+time for both in epoch format (assuming that is best). KRBEXPDATE=klist | grep -i krbtgt | awk '{print $3}' KRBEXPTIME=klist | grep -i krbtgt | awk '{print $4}' CURREPOCH=date +%s KRBEXPEPOCH=`date --date="$KRBEXPDATE $KRBEXPTIME" +%s

I want to see if the kerberos ticket is expired in an if statement, I am having trouble with the logic on if statement working with time. I may have answered my own question here, but should it be CURRENTEPOCH is greater than KRBEXPEPOCH then echo "ticket is not valid". Does this make sense?

if [[ "$CURREPOCH > "$KRBEXPEPOCH" ]] ;
then
  echo "Ticket is not valid"
fi
Blake Russo
  • 88
  • 1
  • 1
  • 7