Write a shell script to modify “cal” command to display calendars of the specified months.

 Write a shell script to modify “cal” command to display calendars of the specified months.


CODE:

#!/bin/bash

# Check if at least one argument is provided
if [ $# -eq 0 ]; then
    echo "Usage: $0 <month1> [month2 month3 ...]"
    exit 1
fi

# Loop through each provided month and display the calendar
for month in "$@"; do
    # Check if the input is a valid month number (1-12)
    if ! [[ $month =~ ^[1-9]|1[0-2]$ ]]; then
        echo "Invalid month number: $month. Month should be between 1 and 12."
        exit 1
    fi

    # Display the calendar for the specified month
    cal $month
    echo "------------------------------------------"
done

Save this script in a file, for example, custom_cal.sh, and make it executable:


chmod +x custom_cal.sh


You can then run the script by providing one or more month numbers as command-line arguments:

./custom_cal.sh 1 3 7

OUTPUT:


    January 2023      
Su Mo Tu We Th Fr Sa  
 1  2  3  4  5  6  7  
 8  9 10 11 12 13 14  
15 16 17 18 19 20 21  
22 23 24 25 26 27 28  
29 30 31              
------------------------------------------
     March 2023       
Su Mo Tu We Th Fr Sa  
          1  2  3  4  
 5  6  7  8  9 10 11  
12 13 14 15 16 17 18  
19 20 21 22 23 24 25  
26 27 28 29 30 31     
------------------------------------------
      July 2023       
Su Mo Tu We Th Fr Sa  
                   1  
 2  3  4  5  6  7  8  
 9 10 11 12 13 14 15  
16 17 18 19 20 21 22  
23 24 25 26 27 28 29  
30 31                
------------------------------------------