The following bash script iterates over dates in a range.

#!/bin/bash
 
# $1 = start date (e.g.: yyyy-mm-dd)
# $2 = end date
 
# make sure the end date is formatted correctly
end_date=$(date -d "$2" +%Y-%m-%d)
 
# set iteration date to start date and format
iter_date=$(date -d "$1" +%Y-%m-%d)
 
until [[ ${iter_date} == ${end_date} ]]; do
    # print the date
    echo ${iter_date}
    # advance the date
    iter_date=$(date -d "${iter_date} +1 day" +%Y-%m-%d)
done

I’ve saved that in a file called “loopdates.sh” and chmod the file to 0755. An example usage follows.

$ ./loopdates.sh 2014-11-27 2014-12-02
2014-11-27
2014-11-28
2014-11-29
2014-11-30
2014-12-01
Posted in Bash
Share this post, let the world know

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">