Converting number to locale's abbreviated month name
Is there any command to convert an integer (1-12) to (Jan-Dec)?
E.g. I pass in input parameter 3, it will return "Mar" Thx, Tsi Lok |
I'll write on for you.
#!/bin/bash ##filename calendar ##converts integers 1-12 to Gregorian calendar month names if echo "$1" | grep '12' then echo "December" exit 0 fi if echo "$1" | grep '11' then echo "November" exit 0 fi if echo "$1" | grep '10' then echo "October" exit 0 fi if echo "$1" | grep '9' then echo "September" exit 0 fi if echo "$1" | grep '8' then echo "August" exit 0 fi if echo "$1" | grep '7' then echo "July" exit 0 fi if echo "$1" | grep '6' then echo "June" exit 0 fi if echo "$1" | grep '5' then echo "May" exit 0 fi if echo "$1" | grep '4' then echo "April" exit 0 fi if echo "$1" | grep '3' then echo "March" exit 0 fi if echo "$1" | grep '2' then echo "February" exit 0 fi if echo "$1" | grep '1' then echo "January" exit 0 fi echo "This program outputs month names for integer inputs between 1 and 12. Please make adjustments as necessary" exit 1 Copy and paste this to a file named calendar. do chmod a+rx calendar mv calendar /usr/local/bin to run the program: romulus4:/home/sam# calendar 12 |
This shell script uses the date command to ouput the locale's abbreviated month name, when you give the argument as a number (1-12) to the command line
#!/bin/bash date -d "01-$1-01" +%b |
I assume from your title that you would like it to be locale independent. Since the man page for date says it works in the locale, it is just the thing to use; not to mention a case statement:
Code:
#! /bin/bash Code:
chmod 755 month.sh The core command is the same, though. |
If you want to have some fun w/ locales, run:
Code:
set | grep ^LC Code:
LC_ALL=de_DE |
All times are GMT -5. The time now is 03:42 PM. |