Tuesday, 11 June 2013

Shell script to accept a number from the user and display the number of days in that particular month.

Code:
echo Enter month
month=(30 31 29)
read mon
case "$mon" in
        [jJ][aA][nN]*) echo "No.of days : ${month[1]} ";;
        [fF][eE][bB]*) echo enter year
                       read year
                       let rem="$year % 4"
                       if [ $rem -eq 0 ] ; then
                          echo "No. of days : ${month[2]}"
                       else
                          echo "No. of days : 28 "
                       fi;;
         [mM][aA][rR]*) echo "No. of days : ${month[1]}";;
         [aA][pP][rR]*) echo "No. of days: ${month[0]}";;
         [mM][aA][yY]*) echo "No. of days: ${month[1]}";;
         [jJ[uU][nN]*)  echo "No. of days: ${month[0]}";;
         [jJ][uU][lL]*) echo "No. of days: ${month[1]}";;
         [aA][uU][gG]*) echo " No. of days: ${month[1]}";;
         [sS][eE][pP]*) echo " No. of days: ${month[0]}";;
         [aA][cC][tT]*) echo "No. of days: ${month[1]}";;
         [nN][oO][vV]*) echo "No. of days: ${month[0]}";;
         [dD][eE][cC]*) echo "No. of days: ${month[1]}";;
esac

Output:
[ty2011330@localhost ~]$ vi month.sh
[ty2011330@localhost ~]$ sh month.sh
Enter month
feb
enter year
2005
No. of days : 28

No comments:

Post a Comment