I believe this shell script will solve the problem. I was hoping for any UNIX built in functions to do the job. Because there is no point in reinventing the wheel. Please feel free to cut short my algorithm for a better code. And if there is any built in functions please inform me.
Currently the output is in DDMMYY format. if you want it in DDMMYYYY format just edit the below line in the code & remove the 'cut' section.
year=`echo $3 | cut -c3-4`
Thanks for all the support from the UNIX forum members
1. The script cecho will echo the output in color so if you dont have cecho replace it by echo. I believe i got cecho from someone in UNIX Forum.
2. The anflg is my script to check whether the date contains alphabetic or alphanumeric characters. You can comment the calls to validation functions to bypass calls to external scripts, but make sure the input is in correct format.
3. The real algorithm lies in the last for loop,
Generate Dates . This for loop will take the dates from unix
cal function and generates the date
The name for the source code in gendt and run them as
gendt <From date> <To date>
Bye.
HTML Code:
# gendt - DATE GENERATOR WITHIN THE GIVEN RANGE
#
# USAGE
#
# gendt <From> <To>
#
# Note: 1. The date must be in DDMMYYYY format
# 2. The <From> date must be smaller than <To> date
# Ex: dt 30012009 28022009
# 3. The output is in DDMMYY format
# 4. Invalid month days like 300209 (feb has 28 days) & 310408
# are corrected automatically by the script
#
# MAINTENANCE HISTORY
# DATE AUTHOR AND DETAILS
# 30-01-09 BAS TR - ORIGINAL
#
#----------------------------------------------------------------------------79
frmDate=$1
toDate=$2
# Basic data check
dateChk1() {
len=`echo $1 | wc -c`
len=`expr $len - 1`
if [ $len -ne 8 ]; then
cecho green "Invalid date length (DDMMYYYY)."
exit
fi
alpNumFlg=`anflg "$1" "AN"`
alpFlg=`anflg "$1" "AT"`
finFlg=`expr $alpNumFlg + $alpFlg`
if [ "$finFlg" != "0" ]; then
cecho green "$1 is not a number"
exit
fi
}
dateChk1 "$frmDate"
dateChk1 "$toDate"
# Basic date validation
dateChk2 () {
case $2 in
"M")
if [ $1 -le 12 ]; then
if [ $1 -le 0 ]; then
cecho green "$1 is an invalid month (1-12)"
exit
fi
else
cecho green "$1 is an invalid month (1-12)"
exit
fi
;;
"D")
if [ $1 -le 31 ]; then
if [ $1 -le 0 ]; then
cecho green "Day value $1 must be within (1-31)"
exit
fi
else
cecho green "Day value $1 must be within (1-31)"
exit
fi
;;
"Y")
if [ $1 -le 0 ]; then
cecho green "$1 is an invalid year."
exit
fi
;;
esac
}
frmDtDay=`echo $frmDate | cut -c1-2`
frmDtMon=`echo $frmDate | cut -c3-4 | tr -d 0`
frmDtYer=`echo $frmDate | cut -c5-8`
toDtDay=`echo $toDate | cut -c1-2`
toDtMon=`echo $toDate | cut -c3-4 | tr -d 0`
toDtYer=`echo $toDate | cut -c5-8`
dateChk2 "$frmDtDay" "D"
dateChk2 "$frmDtMon" "M"
dateChk2 "$frmDtYer" "Y"
dateChk2 "$toDtDay" "D"
dateChk2 "$toDtMon" "M"
dateChk2 "$toDtYer" "Y"
# Make any D & M day,month formats to DD,MM respectively
dateFill() {
val="$1"
dtLen=`echo "$val" | wc -c`
dtLen=`expr $dtLen - 1`
if [ $dtLen -lt 2 ]; then
val="0$1"
fi
return $val
}
yerDiff=`expr $toDtYer - $frmDtYer`
if [ $yerDiff -lt 0 ]; then
cecho green "Date $frmDate must be less than date $toDate."
exit
fi
# Display the date in DDMMYY format
datDisp () {
dateFill $1
dayTmp=$val
dateFill $2
monTmp=$val
year=`echo $3 | cut -c3-4`
echo "$dayTmp$monTmp$year"
}
# Generate dates
for y in $(seq $frmDtYer $toDtYer); do
for m in $(seq 1 12); do
dtGenIni=`cal $m $y | grep -v [A-z] | tr -s ' ' '\n'`
for d in $dtGenIni; do
day=$d
# same year
if [ $y -eq $toDtYer ]; then
# same month
if [ $m -eq $toDtMon ]; then
# same day
if [ $day -eq $toDtDay ]; then
# if all the above, print the same BAS-BAS-BAS day
datDisp $day $m $y
# different days
else
# print days between From & To Days
if [ $day -ge $frmDtDay ]; then
if [ $day -le $toDtDay ]; then
datDisp $day $m $y
fi
fi
fi
# different months
else
# print days between From & To Months
if [ $m -ge $frmDtMon ]; then
if [ $m -le $toDtMon ]; then
# if curent month is From or To,
# remove dates outside the range
if [ $m -eq $frmDtMon ]; then
if [ $day -ge $frmDtDay ]; then
datDisp $day $m $y
fi
else
datDisp $day $m $y
fi
fi
fi
fi
# different year
else
# print the days between the From & To Years
if [ $y -ge $frmDtYer ]; then
if [ $y -le $toDtYer ]; then
# check for same From year
if [ $y -eq $frmDtYer ]; then
# check for same month
if [ $m -eq $frmDtMon ]; then
if [ $day -ge $frmDtDay ]; then
datDisp $day $m $y
fi
fi
fi
# check for same To Year
if [ $y -eq $toDtYer ]; then
# check for same month
if [ $m -eq $toDtMon ]; then
datDisp $day $m $y
fi
else
# simply print other dates within the range
datDisp $day $m $y
fi
fi
fi
fi
done
done
done