Coditional Labs of the month number

Hi all,

I was doing the lab of the month number which is:

" Develop a shell script /home/bob/print-month-name.sh that accepts the number of a month as input and prints the name of the respective month. eg ./print-month-name.sh 1 should print January and ./print-month-name.sh 5 should print May . Also keep these in mind.

  1. The script must accept a month number as a command line argument.
  2. If a month number is not provided as command line argument, the script must exit with the message No month number given .
  3. The script must not accept a value other than 1 to 12 . If not the script must exit with the error `Invalid month"

So I created this script, but I want to know if it is valid or not, I already saw the solution and I saw some diference only in the logic, I think maybe when I nest the if inside other if, I am creating the error but I am new on this, please can you check it and tell me if it is correct?

My script is:

A=$1

if [[ A -gt 1 && A -lt 13 ]]
then
        if [ A -eq 1 ]
        then
                echo "January"
        elif [ A -eq 2 ]
        then
                echo "Febrauary"
        elif [ A -eq 3 ]
        then
                echo "March"
        elif [ A -eq 4 ]
        then
                echo "April"
        elif [ A -eq 5 ]
        then
                echo "May"
        elif [ A -eq 6 ]
        then
                echo "June"
        elif [ A -eq 7 ]
        then
                echo "July"
        elif [ A -eq 8 ]
        then
                echo "August"
        elif [ A -eq 9 ]
        then
                echo "September"
        elif [ A -eq 10 ]
        then
                echo "Octuber"
        elif [ A -eq 11 ]
        then
        elif [ A -eq 12 ]
        then
                echo "December"
elif ! [[ "$A" =~ ^[0-9]+$ ]]
    then
        echo "No month number given"
else
        echo "Invalid month number given"
fi

Thank you.

@Cristian-Gomez-Arand
Yes, it seems that it’s correct but there are some syntax errors. check this

A=$1

if [[ $A -gt 1 && $A -lt 13 ]]
then
        if [ $A -eq 1 ]
        then
                echo "January"
        elif [ $A -eq 2 ]
        then
                echo "Febrauary"
        elif [ $A -eq 3 ]
        then
                echo "March"
        elif [ $A -eq 4 ]
        then
                echo "April"
        elif [ $A -eq 5 ]
        then
                echo "May"
        elif [ $A -eq 6 ]
        then
                echo "June"
        elif [ $A -eq 7 ]
        then
                echo "July"
        elif [ $A -eq 8 ]
        then
                echo "August"
        elif [ $A -eq 9 ]
        then
                echo "September"
        elif [ $A -eq 10 ]
        then
                echo "Octuber"
        elif [ $A -eq 11 ]
        then
                echo "November"
        elif [ $A -eq 12 ]
        then
                echo "December"
        fi
elif ! [[ "$A" =~ ^[0-9]+$ ]]
    then
        echo "No month number given"
        exit
else
        echo "Invalid month number given"
        exit

fi
1 Like

Thank you, I did not add a fi.

@Cristian-Gomez-Arand , Glad to hear that the issue has been resolved.