BASH looping though time

2019-09-16 05:44发布

问题:

Using BASH I want to loop from a start to end date at ten-minute intervals. I tried

begin_date="2015-01-01 00:00:00"
end_date="2015-02-20 00:00:00"
d=$begin_date
while [ "$d" != "$end_date" ]; do
    echo $d
    d=$(date -d "${d} + 10 min" +"%Y-%m-%d %H:%M")
done

But it didn't work. Looking at Bash:Looping thru dates

#This works
d=$(date -I -d "${d} + 1 day")

#This doesn't work
d=$(date -d "${d} + 1 day" +"%Y-%m-%d")

What am I missing in the format string?

回答1:

The example you linked to just needs to be adjusted slightly:

#!/bin/bash

## User-specified dates.
# See [GNU Coreutils: Date] for more info
# [GNU Coreutils: Date]: https://www.gnu.org/software/coreutils/manual/html_node/Combined-date-and-time-of-day-items.html#Combined-date-and-time-of-day-items
begin_date="2015-01-01T00:00"
end_date="2015-01-01T00:40"

# Run through `date` to ensure iso-8601 consistency
startdate=$(date --iso-8601='minutes' --date="${begin_date}")
enddate=$(date --iso-8601='minutes' --date="${end_date}")

# Do loop
d="$startdate"
while [ "$d" != "$enddate" ]; do 
  echo $d
  d=$(date --iso-8601='minutes' --date="$d + 10 minutes")
done

Note that the options -I and -d are equivalent to --iso-8601 and --date respectively.



回答2:

The expression date -d "${d} + 10 min" seems not to produce a date with an offset of 10 minutes. In fact, when I run your code, I see a date counter going backwards. (Posting this diagnostic as part of your question would help others see where the problem is; you should not require others to run your code just to see what it does.)

Anyway, the sane way to do this is to convert the dates to Unix epoch, then take it from there.

for ((d=$(date -d "$begin_date" +%s); d <= $(date -d "$end_date" +%s); d += 600))
do
    date -d @$d +"%F %H:%M"
done

Doing date arithmetic in the shell is probably going to be rather inefficient; converting this to e.g. Awk or Perl might be worth your time if you find it's too sluggish, or need to run it lots of times.