Print every date between a Time duration
- I would grab your date1 and date2 and convert them to timestamps
- Then I would investigate: whats 1 hour converted to timestamp.
- And write a for-loop from timestamp(date1) to timestamp(date2)
within the loop I would add step by step timestamp(1 hour), convert the new timestamp back in the prefered date/time format and print it out until the loop ends.
# by user85321 @ ask ubuntu
# call: ./script.sh 20120205_16 20120205_16 20120305_18
# STEP 1:
# set userdefined Dateformat to normal
d1=$(echo "$1" | sed 's/_/ /g')
d2=$(echo "$2" | sed 's/_/ /g')
# set normalized dates to timestamps
t1=$(date --utc --date "$d1" +%s)
t2=$(date --utc --date "$d2" +%s)
# STEP 2:
# time increment, 1h = 3600 s
# STEP 3:
if [ $t2 -gt $t1 ]
for i in $(seq $t1 $h $(($t2-$h)))
# take this and do what you want to do ;o)
echo $(date --date="@$i" "+%Y%m%d_%H")
My bash prints out exactly that list , what you described . So, it is done.
I think some cracks would do it shorter/ better, but it works ...