Monday, August 13, 2012

Cycling date in bash

Maybe you want to cycle a time frame from a day to another, taking in consideration how many days a month have (also for leap years). Here is how you can do in bash.

Date format is: YYYYMMDD

#!/bin/bash

b=$1
e=$2

while [ "$b" -le "$e" ]
do
   echo $b
   b=$(date +%Y%m%d -d "$b +1 day")
done




Execute it

./loopdate.sh 20090101 20090301


Result

20090101
20090102
...
20090226
20090227
20090228
20090301


It works on Linux, and using gdate (from opencsw, blastwave) on Solaris. On Mac OS X it doesn't work.

No comments:

Post a Comment