Checking for future / non existent dates

'm attempting to script an application for the bash shell. The application needs to check for birthday, but must check the birthday to see if the date is a) in the future b) exists at all (ie Feb 29th during non-leap years). The input is being entered in a YYYYMMDD format, so I was hoping someone could point me in the direction for how to check for future dates and non-existent dates.

Thanks

  • D

Korn:

#! /usr/bin/ksh

DATE=$1
typeset -i day

eval $(echo $DATE | sed 's/^\(....\)\(..\)\(..\)/year=\1 month=\2 day=\3/')

cal $month $year 2> /dev/null | grep -w $day > /dev/null
if [[ $? -eq 0 ]] ; then
        echo "Valid Date"
else
        echo "Invalid Date"
fi

or same thing in bash

#! /bin/bash

DATE=$1
declare -i day

eval $(echo $DATE | sed 's/^\(....\)\(..\)\(..\)/year=\1 month=\2 day=\3/')

cal $month $year 2> /dev/null | grep -w $day > /dev/null
if [[ $? -eq 0 ]] ; then
        echo "Valid Date"
else
        echo "Invalid Date"
fi

This script will validate the format and checks for the date availability in the calendar (thus solves leap year problem). But as the user expected it will not check whether the date falls on future or not. So the user has to take care of it in his script like this:

if [ "`date '+%Y%m%d'`" -ge "$DATE" ]; then
.....
fi