Home > bash > bash – Print dates in a range.

bash – Print dates in a range.

Problem:
Needed to have a range of dates for a script.

Solution:
Opted to write it in bash.

How it works:

shell> bash list_dates.sh "2011-07-02" "2011-07-10"
2011-07-02
2011-07-03
2011-07-04
2011-07-05
2011-07-06
2011-07-07
2011-07-08
2011-07-09
2011-07-10

Source:

#!/bin/bash
#=============================================================================
#
# FILE: list_dates.sh
#
# USAGE: list_dates.sh start_date end_date
#
# DESCRIPTION: List the dates between a given range of dates.
#
#=============================================================================

start_date="$1"
end_date="$2"

#=== FUNCTION ================================================================
#           NAME: usage
#    DESCRIPTION: Display usage information for this script.
#    PARAMETER 1: ---
#=============================================================================
function usage()
{
cat <<- EOT
Lists the dates between a given range of dates.
Dates must be in yyyy-mm-dd format

usage: $0 start_date end_date [--help]
    start_date (YYYY-MM-DD) -- First date in the range. 
    end_date (YYYY-MM-DD) -- Last date in the range. 
    help -- display this message
EOT
}

#-----------------------------------------------------------------------------
# Display usage
#-----------------------------------------------------------------------------
if [ "$1" == "--help" ]
then
    usage
    exit
fi
#-----------------------------------------------------------------------------
# Check the date formats
#-----------------------------------------------------------------------------
start_date=$(date --date="$start_date" +"%F") # Defaults to current date if none supplied.
if [ "$?" -eq "1" ] || [ -z "$start_date" ]
then
    echo "Please provide a start date in this format (YYYY-MM-DD)"
    exit
fi

end_date=$(date --date="$end_date" +"%F") # Defaults to current date if none supplied.
if [ "$?" -eq "1" ] || [ -z "$end_date" ]
then
    echo "Please provide an end date in this format (YYYY-MM-DD)"
    exit
fi

# Make sure that the first day is always before the second date.
# If a user enters 2012-09-08 2011-02-10 switch the dates to be
# 2011-02-10 2012-09-08
file_=$(mktemp)
printf "%s\n%s" $start_date $end_date | sort > $file_
start_date=$(head -n 1 $file_)
end_date=$(tail -n 1 $file_)
rm $file_
#-----------------------------------------------------------------------------
# main
#-----------------------------------------------------------------------------
i=0
while [ "$date_" != "$end_date" ]
do
    date_=$(date --date="$start_date +$i day" +"%F")
    echo $date_
    i=$(expr $i + 1)
done

Update 2012-11-06:
Changed

date_=$(date --date="$(date --date=$start_date +%F) + $i day" +"%F")

to

date_=$(date --date="$start_date +$i day" +"%F")
Categories: bash Tags:
  1. jackrabbit
    November 5, 2012 at 2:55 pm

    thanks for this script..
    I think you could have kept the main command shorter:

    date_=$(date -d “$start_date +$i days”)

    • November 6, 2012 at 4:29 am

      Thanks, I have updated the script.

  1. No trackbacks yet.

Leave a reply to Rodnee Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.