Manoj.V
Manoj.V

Reputation: 37

Multiple dates are not working with epoch comparison in bash

printf "%s\n" "$EXPDATES"

Jul 12 2019 12:00:00
Jul 12 2019 12:00:00
Jul 12 2019 12:00:00
Jun 18 2019 12:00:00

Aug 8 2019 12:00:00
May 8 2018 00:00:00

The above o/p I'm getting from "for loop" and passing all the dates (including one empty line) in same format to another "for loop" to convert and compare with current "epoch".

curr_epoch=$(date +%s)

for expdate in "${EXPDATES[@]}"; do
  exp_epoch=$(date +%s -d "$expdate")
  if (( curr_epoch > exp_epoch )); then
    echo "$expdate in future."
  else
    echo "$expdate in past."
  fi
done

Here I'm not getting proper output for all the dates. "$expdate" in echo line doesn't return anything.

I'm not sure whether for-loop is comparing all the dates.

Can anyone please tell me how to compare all the dates and show output with all the dates compared?

Upvotes: 0

Views: 86

Answers (1)

Stefan M
Stefan M

Reputation: 889

You are missing ending quotes for the echo lines in your code. This will work:

for expdate in "${EXPDATES[@]}"; do
  exp_epoch=$(date +%s -d "$expdate")
  if (( curr_epoch > exp_epoch )); then
    echo "$expdate in future."
  else
    echo "$expdate in past."
  fi
done

Upvotes: 1

Related Questions