LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Converting number to locale's abbreviated month name (https://www.linuxquestions.org/questions/linux-newbie-8/converting-number-to-locales-abbreviated-month-name-376561/)

tsilok 10-24-2005 09:14 PM

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

AwesomeMachine 10-25-2005 01:12 AM

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

jdiemer 10-25-2005 11:53 AM

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

archtoad6 10-25-2005 04:53 PM

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
#0 month.sh === print locale month abbr. from number
#@ by f.a.archibald.iii        all rights reserved

_usage () {
cat << end_usage

month.sh    print locale month abbr. from month number
    Syntax:  month.sh <number_of_month>
   
end_usage
}

case $1 in
  [1-9]|1[0-2])                date -d "$1/01" +%b        ;;
  *)                        _usage ;  exit 1
esac

And here is some test code:
Code:

chmod 755 month.sh

for ((x=0;$x<14;x++)) do
  echo -ne "$x\t"
  ./month.sh $x
  echo -e "\033[A\t\t exit code is  $?"
done

EDIT: Drat, jdiemer posted while I was composing & testing.
The core command is the same, though.

archtoad6 10-25-2005 05:18 PM

If you want to have some fun w/ locales, run:
Code:

set |  grep ^LC
& record your current locale; then run any or all of the following (followed by my test code above):
Code:

LC_ALL=de_DE
LC_ALL=fr_FR
LC_ALL=pt_BR
LC_ALL=en_US

Or any others you have installed. Just be sure to restore your own when you are done.


All times are GMT -5. The time now is 03:42 PM.