I need some help here. I stuck in my programming in manipulation of my date.
Date is in format of DD-MON-YY and I want to check it if the user input the correct date format. So I create a function to test the date, the month and the year separately, but the function also read the “-” in the format so I decided to use this code:
echo -n " Date [ DD-MON-YY ]:" #user will input the date
read dates
vardate=$ echo "$dates" | tr "-" " "
echo $vardate
I use that so there will be no problem with “-” character when I call the function.
but the problem now is how can I save the 3 parts of the date [day, month, year]?? so my function can evaluate it properly.
My function:
datefxn(){
d1 = $date1
d2 = $month1
d3 = $year1
########## check the DAY #####################
test ${#d1} -eq 2 || s=" INVALID!"
if [ "$d1" -gt 0 ] && [ "$d1" -le 31 ]
then
break ;;
else
echo "Month is OUT OF RANGE!"
fi
########## check the DAY #####################
test ${#d2} -eq 3 || s=" INVALID!"
MONTH=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
echo "${MONTH[@]}"
for element in "${MONTH[@]}"
do
if [[ $element == $d2 ]]
then
echo " a "
break
else
echo " WAAA "
fi
done
########## check the YEAR #####################
test ${#d3} -eq 2 || s=" INVALID!"
} #end datefxn()
Something like this:
The above solution is given to fix your code.
Update:
A better approach would be like this one using arrays and in-place substitution: