Headlines
Loading...
Program in UNIX to validate the date

Program in UNIX to validate the date

Write a shell script to validate the date

echo "Enter date (dd-mm-yyyy) :"
read da
IFS="-"
n="[0-9][0-9]"
month=(0 31 29 31 30 31 30 31 3 30 31 30 31)

case $da in
          " ") echo "Date not entered ";;
   $n-$n-$n$n) set $da

               if [ `expr $3 % 4` -eq 0 -a \( `expr $3 % 400` -eq 0 -o `expr $3 % 100` -ne 0 \) ]
               then
                   leap=1
               else
                   leap=0
               fi
               if [ $2 -gt 12 -o $2 -eq 0 ]
               then
                   echo "invalid month"
                   exit
               else
                   case $da in
                     29-02-????)
                                 if [ $leap -eq 1 ]
                                 then
                                     break
                                 else
                                     echo "Invalid date"
                                     exit
                                 fi ;;
                             *)
                                if [ $1 -gt ${month[$2]} -o $1 -eq 0 ]
                                then
                                     echo invalid day
                                     exit
                                fi ;;
                   esac
               fi;;
           *) echo "invalid date"
              exit
esac
 echo “$1-$2-$3 is a valid date”
*** PLEASE checkout the Best deals from for top sites like Amazon, Flipkart etc ***