LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-24-2005, 09:14 PM   #1
tsilok
LQ Newbie
 
Registered: Sep 2005
Posts: 6

Rep: Reputation: 0
Question 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
 
Old 10-25-2005, 01:12 AM   #2
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
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
 
Old 10-25-2005, 11:53 AM   #3
jdiemer
LQ Newbie
 
Registered: Aug 2004
Distribution: Redhat Enterprise
Posts: 15

Rep: Reputation: 0
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
 
Old 10-25-2005, 04:53 PM   #4
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
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.

Last edited by archtoad6; 10-25-2005 at 04:59 PM.
 
Old 10-25-2005, 05:18 PM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
transform month number to month name in php ALInux Programming 1 11-09-2005 10:45 AM
Have problem converting a decimal number to octal Linh Programming 4 05-20-2004 03:21 PM
Debian Locale's boardinglaxplr Linux - Newbie 4 05-17-2004 02:35 PM
Starting day of month, month length chrisk5527 Programming 2 03-03-2004 04:03 PM
getting last locale's abbreviated month name through bash scripting markus1982 Programming 2 05-10-2003 08:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration