Open In App

Shell Script to Enhance the Calendar to Accept MM, MMM, YYYY

Last Updated : 23 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

 We are going to write a shell script to enhance the calendar to accept any month as MM or MMM or only year as YYYY or both month and year. Our shell script will accept at least one argument which will be either month or year. Maximum 2 arguments will be used which will be month and year. An enhanced calendar will be displayed using the specified arguments. We are going to use the cal and ncal command in Linux to display calendars.

Method 1: Using cal command

If a user wants a quick view of the calendar in the Linux terminal, cal is the command for you. By default, the cal command shows the current month calendar as output.

The cal command in Linux is used to view the calendar for a specific month or for the entire year. A single argument in the cal command defines the four-digit year (1-9999) to be shown. Month (1-12) and Year(1 – 9999) is represented by two parameters. If no arguments are given, then the current month is shown. 

We can use the following command to know more about cal utility in Linux:

man cal

For example:

Approach:

  1. Start
  2. Check whether arguments are passed or not
  3. If no arguments are passed then display “Invalid arguments and exit program”
  4. If 2 arguments are passed then
  5. check whether 1st arguments are greater than 12 and the second argument is greater than 2021 (current year).
  6. If it is greater, then display”invalid year or month” and go to step 12
  7. Else if only one argument is passed then check whether it is greater than 12.
  8. If it is greater than 12 then it is considered a year.
  9. Then display a calendar of the specified year.
  10. else if the argument is smaller than 12 then the passed argument is a month.
  11. Display calendar of the specified month of the current year.
  12. Exit.

Below is the implementation using cal command:-

# Shell Script to Enhance the Calendar to Accept 
# MM, MMM, YYYY using cal command

# check whether arguments are passed or not
if [ $# -eq 0 ]
then

        # if arguments are not passed then display this
        echo "Invalid Arguments"
        
        # exit the program
        exit 
fi 

# if 2 arguments are passed
if [ $# -eq 2 ]    
then

    # if argument 1 is greater than 12 or argument 2 
    # is greater than 2021
    if [ $1 -gt 12 -o $2 -gt 2021 ]
    then
    
        # then display invalid month or year
        echo "invalid Year or month"
        
    # else display calendar of the specified month 
    # and year
    else
        ncal $1 $2    
    fi
    
# if only one argument is passed then
else if [ $# -eq 1 ]
then

    # if argument is greater than 12
    if [  $1 -gt 12 ]
    then
        cal $1 # display calendar of specified year
        
    # else display calendar of specified month
    else
        case $1 in #start switch case
            01) m = jan;;
            02) m = feb;;
            03) m = mar;;
            04) m = apr;;
            05) m = may;;
            06) m = jun;;
            07) m = jul;;
            08) m = aug;;
            09) m = sep;;
            10) m = oct;;
            11) m = nov;;
            12) m = dec;;
        esac # end switch case
        echo \" Calendar for $1 Month : \"
        
        # display calendar of specified month using -m
        cal -m $1

    fi
fi
fi

Output:

Method 2: Using ncal command

The ncal command can also be used to display the calendar. The ncal command has the same functionality as cal, except it may show the calendar vertically with weeks in columns instead of horizontally. We can use the following command to know more about cal utility in Linux:

man ncal

For example:

In the above examples, In the cal or ncal command, if the user types the “cal 2” command in the terminal then, it will display the whole calendar of year 0002. But to enhance the calendar, if the user enters 2 as an argument then the second month of the current year should be displayed. We will use the cal/ncal utility to Enhance the Calendar to Accept MM, MMM, YYYY. 

Below is the implementation using ncal command:-

# Shell Script to Enhance the Calendar to Accept MM, MMM, YYYY
# using ncal command

# check whether arguments are passed or not
if [ $# -eq 0 ]
then

        # if arguments are not passed then display this
        echo "Invalid Arguments"  
        
        # exit the program
        exit 
fi 

if [ $# -eq 2 ]    # if 2 arguments are passed
then

    # if argument 1 is greater than 12 or argument 2 is 
    # greater than 2021
    if [ $1 -gt 12 -o $2 -gt 2021 ]     
    then
    
        # then display invalid month or year
        echo "invalid Year or month"    
        
    # else display calendar of the specified month and year    
    else
        ncal $1 $2    
    fi
else if [ $# -eq 1 ] # if only one argument is passed then

    if [  $1 -gt 12 ]    # if argument is greater than 12
    then
        cal $1 # display calendar of specified year
    else     # else display calendar of specified month
        case $1 in #start switch case
            01) m = jan;;
            02) m = feb;;
            03) m = mar;;
            04) m = apr;;
            05) m = may;;
            06) m = jun;;
            07) m = jul;;
            08) m = aug;;
            09) m = sep;;
            10) m = oct;;
            11) m = nov;;
            12) m = dec;;
        esac # end switch case
        echo \" Calendar for $1 Month : \"
        ncal -m $1 # display calendar of specified month using -m

    fi
fi
fi
#end if

Output:

ncal command will display a calendar in a more enhanced way.

In the script, if the user enters one argument then we will check whether it is greater than 12, if it is greater than 12 then the argument will be considered as a year else argument will be considered as a month. If no argument is given, then “Invalid arguments is displayed.”. If 2 arguments are given then it will denote a month and a year. 

To run the shell script use:

./cal.sh <argument 1> <argument 2>

Example:

./cal.sh 4
./cal.sh 8 2021
./cal.sh 1999

Give execution permission to shell script using:

chmod +x cal.sh

Hence the above Shell Script will Enhance the Calendar to Accept MM, MMM, YYYY.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads