Small DateValidation bash script
Below is a program I'M trying to write that will take an 8 digit date and validate whether or not it's a valid date. I'M very new to shell scripting so I really a have no idea what I'M going. Could you take a look and let me know what all I'M doing wrong here? Thanks.
Code:
#! /bin/bash -x
# 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
export MM=${DATE:0:2}
export DD=${DATE:2:2}
export YY=${DATE:4:4}
if [ ${YY} -gt 0 ]; then #{
if [ ${MM} -gt 0 & < 13 ]; then #{
if [ ${MM} -eq 2 ]; then #{
if [ is_leap_year ${YY} == 1 ]; then #{
if [ ${DD} -gt 0 & -le 29 ]; then #{
elif [ ${DATE} -gt 0 & -le ${CALENDER[${MM}]}] #}}}}}}
echo "${DATE} is a valid date"
else
"${DATE} is an invalid date"
fi #}
#############################
### FUNCTION-is_leap_year ###
#############################
is_leap_year() {
if [ 0 -eg ${YY} % 4 & 0 -ne {YY} % 100 or 0 -eq ${YY} % 400 ]; then #{
return true #}
else #{
return false #}
exit
#END
Human engineer your script.....
1. I presume you have a reason for using the shell, rather than some more powerful scripting language such as perl or python?
2. Once you get your script working, may I suggest a bit of human engineering?
a) validate the input. i.e. months 1-12, etc.
b) allow user to enter dates with / or - as in mm-dd-yyyy
c) allow user to enter dates with yy
d) It always annoys me (as a non USA resident) when programs assume the date is mm-dd-yyyy. This seems to me to be a very illogical format. It is neither ascending order (dd-mm-yyyy) nor descending order. It is not that difficult to allow yyyy/mm/dd or yyyy-mm-dd which AFAIK is the international ISO standard way of doing dates, (as well as mm-dd-yyyy which is what your USA users will probably assume.).
Just my two cents worth.......
pgmer6809