Grimlockz
Grimlockz

Reputation: 2581

Script to echo out range of dates

I've been trying to get a script to print out a range of dates from a start date then the day after up to the present day.

Example:

Backup-2011-06-14
Backup-2011-06-15
Backup-2011-06-16

Had a idea below but doesn't even slightly work, any ideas?

start=$(date +%F --date="2011-06-14")
echo "$start"
current=$(date +%F)
echo "$current"
end=$(date +%F)

while [ "$start" != "$current" ]; do
    echo backup-$(( $(date --date="$start" +%F) + $(date +%s --date='1 day') ))
done

Upvotes: 2

Views: 1832

Answers (1)

Jonathan Hall
Jonathan Hall

Reputation: 79556

Date math in bash is difficult. But the date command does support simple addition or subtraction from today's date. So a different approach is necessary in your case. First, determine the number of days for which you want to output these dates, then output the dates as they relate to today's date.

#!/bin/bash
days=$(( ($(date +%s)-$(date +%s --date="2011-06-14"))/86400 ))

while [ $days -gt 0 ]; do
    echo backup-$(date --date="-$days days" +%F)
    days=$(($days-1))
done

Upvotes: 2

Related Questions