This script whould be able to determine if a date that has been entered in as 8 digits is valid or not.
It will first check that there are 8 digits, if so everything is good.
It will then theck that the month value is > 0 & < 13, if so everything is good.
In order to get the day I need to fist find out if it's leap year or not to determin if the maximum number of days is 28 or 29
I use a function "is_leap_year" to determine if it is or is not leap year.
I use a function "get_day" to check that the day is > 0 & <= CALENDER[MM]
For some reason my program is running the "get_day" function even when the function "is_leap_year" returns true, in that case it would only call "get_day" in the else clause of that code block. Please help I am stumped.
Code:
#! /bin/bash -x
#############################
### FUNCTION-is_leap_year ###
#############################
is_leap_year()
{
PART1=`expr ${YY} % 4`
PART2=`expr ${YY} % 100`
if [[ 0 -eq ${PART1} ]] && [[ 0 -ne ${PART2} ]] || [[ 0 -eq `expr ${YY} % 400` ]]; then
result=true
else
result=false
fi
}
####################
### END FUNCTION ###
####################
########################
### FUNCTION-get_day ###
########################
get_day()
{
if [[ ${DD} -gt 0 ]] && [[ ${DD} -le ${CALENDER[${MM}]} ]]; then #{
DAY="GOOD"
else
DAY="BAD"
fi #}
}
####################
### END FUNCTION ###
####################
# This is the calender array for the DateValidation program
CALENDER=(31 28 31 30 31 30 31 31 30 31 30 31)
read -p "Enter a date for validation: " DATE
# establish the varible LEN to hold the number of characters in Date, 8 is the only valid number
LEN=$(echo ${#DATE})
if [ $LEN -eq 8 ]; then #{
# set date dariables MM, DD, & YY
MM=${DATE:0:2}
DD=${DATE:2:2}
YY=${DATE:4:4}
if [ ${YY} -gt 0 ]; then #{
if [ ${MM} -gt 0 ] && [ ${MM} -lt 13 ]; then
if [ "${MM} -ne 02" ]; then
get_day ${DD}
if [ ${DAY} == GOOD ]; then
echo "${DATE} is a valid date!"
else
echo "${DD} is invalid!"
fi #}
else
is_leap_year ${YY}
if [ "${result} == true" ]; then
if [ ${DD} -gt 0 ] && [ ${DD} -le 29 ]; then
echo "${DATE} is a valid date"
else
echo "The day you entered for this date is invalid"
fi #}
else
get_day ${DD}
if [ ${DAY} == GOOD ]; then
echo "${Date} is a valid date"
else
echo "Date ${DD} is invalid"
fi #}
fi #}
fi #}
else
echo "${MM} is invalid!"
fi #}
else
echo "${YY} is invalid!"
fi #}
else
echo "Incalid number of digits for a valid date"
fi #}