Write two bash shell scripts that convert between month names and numbers. The f
ID: 3574906 • Letter: W
Question
Write two bash shell scripts that convert between month names and numbers. The first should be called 05-month-num that uses the bash case construct to determine and display the number of the month (1-12) when a month name (January, February, ..., December) when a month name is provided on the command-line. The month name may be provided either as the full name (see above) or as the 3-character abbreviation (1st 3 characters of the full name). The name must be entered in the upper/lower case format illustrated above. The second script should be called 05-month-name and uses the bash if/elif construct to determine and display the name of the month (January, February, ..., December) when a month number (1-12) is provided on the command-line. Both scripts should perform error-checking on the command-line information, and, if more or less than exactly one single command-line parameter is used then an error (usage) message should be displayed. Error checking on the command-line data and an appropriate message (see below) should be displayed if it is not within an expected set of values. See the following example runs for clarification on how this should work and how the output should look $ 05-month-num
Explanation / Answer
05-month-num.sh
### Validate input
if [ $# -eq 0 ]
then
echo "Usage: $0 InputMonYYYY"
echo "Example: $0 "Oct 2011""
exit 1
fi
### Read input
INPUTSTR=`echo $1 | tr 'a-z' 'A-Z'`
MON_STR=`echo $INPUTSTR`
if [ "$MON_STR" = "JAN" -o "$MON_STR" = "JANUARY" ]
then
MON_NUM=01
elif [ "$MON_STR" = "Feb" -o "$MON_STR" = "FEBRUARY" ]
then
MON_NUM=02
elif [ "$MON_STR" = "Mar" -o "$MON_STR" = "MARCH" ]
then
MON_NUM=03
elif [ "$MON_STR" = "Apr" -o "$MON_STR" = "APRIL" ]
then
MON_NUM=04
elif [ "$MON_STR" = "May" -o "$MON_STR" = "MAY" ]
then
MON_NUM=05
elif [ "$MON_STR" = "Jun" -o "$MON_STR" = "JUNE" ]
then
MON_NUM=06
elif [ "$MON_STR" = "Jul" -o "$MON_STR" = "JULY" ]
then
MON_NUM=07
elif [ "$MON_STR" = "Aug" -o "$MON_STR" = "AUGUST" ]
then
MON_NUM=08
elif [ "$MON_STR" = "Sep" -o "$MON_STR" = "SEPTEMBER" ]
then
MON_NUM=09
elif [ "$MON_STR" = "Oct" -o "$MON_STR" = "OCTOBER" ]
then
MON_NUM=10
elif [ "$MON_STR" = "Nov" -o "$MON_STR" = "NOOVEMBER" ]
then
MON_NUM=11
elif [ "$MON_STR" = "Dec" -o "$MON_STR" = "DECEMBER" ]
then
MON_NUM=12
fi
echo ${MON_NUM}
05-month-name.sh
### Validate input
if [ $# -eq 0 ]
then
echo "Usage: $0 InputMonYYYY"
echo "Example: $0 "2011""
exit 1
fi
### Read input
INPUTSTR=$1
MON_STR=`echo $INPUTSTR`
if [ $MON_STR -gt 12 -o $MON_STR -lt 1 ]
then
echo "enter number vetween 1-12"
fi
if [ $MON_STR -eq 1 ]
then
echo "JANUARY"
elif [ $MON_STR -eq 2 ]
then
echo "FEBRUARY"
elif [ $MON_STR -eq 3 ]
then
echo "MARCH"
elif [ $MON_STR -eq 4 ]
then
echo "APRIL"
elif [ $MON_STR -eq 5 ]
then
echo "MAY"
elif [ $MON_STR -eq 6 ]
then
echo "JUNE"
elif [ $MON_STR -eq 7 ]
then
echo "JULY"
elif [ $MON_STR -eq 8 ]
then
echo "AUGUST"
elif [ $MON_STR -eq 9 ]
then
echo "SEPTEMBER"
elif [ $MON_STR -eq 10 ]
then
echo "OCTOBER"
elif [ $MON_STR -eq 11 ]
then
echo "NOVEMBER"
elif [ $MON_STR -eq 12 ]
then
echo "DECEMBER"
fi
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.