I wrote the following script to try and get validate if a date was valid or not. I think I've about got the script done but I'M having some trouble getting the values from the array. When I input 11291985 into the program I'M getting "29 is invalid" I can't figure out where my logic or syntax is wrong here. All this program is doing is making sure that the month is > 0 & < 13, making sure the year is > 0, then if the months is 2 it checks to see if the year is leap year. If it is leap year the program make sure the day value in the number is > 0 & <=29. If it is not leaop year it runs a function that checks the day against an array containing the maximum number of days for each month. If the day is > 0 and <= the number of days for that month the all is well, otherwise you need to get invalid date. I'M getting invalid date even when supplying it valid dates.

#! /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()
{
    DAY="BAD"
    if [[ ${DD} -gt 0 ]] && [[ ${DD} -le ${CALENDER}[${MM}] ]]; then #{
        DAY="GOOD"
    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 2 ]; 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 [ ${YY} == 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 #}

Recommended Answers

All 2 Replies

hi,

if [ ${YY} = true ]

$YY is never true! only $result may be false or true, not $YY

Thanks. I did fix that one. For whatever reason the program is running the get_day function that's in the else clause even when result == true.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.