开发者

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"开发者_C百科)
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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜