Showing results for 
Search instead for 
Do you mean 

Find last month name

SOLVED
Go to Solution
Highlighted
Honored Contributor [Founder]

Find last month name

Hi All

Help me to find put name of the last month.

i.e. Jan, Feb, Apr format
a warrior never quits
10 REPLIES
Honored Contributor [Founder]

Re: Find last month name

Sorry, typo mistakes.

Please help me to find out the name of last month in shell script. (in POSIX)
a warrior never quits
Acclaimed Contributor [Founder]

Re: Find last month name

You could create an array an index into it:
set -A months Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
this_month=$(date +%m)
(( last_month = (this_month + (12-2)) % 12 ))
echo ${months[last_month]}
Acclaimed Contributor [Founder]

Re: Find last month name

Hi:

If you simply want to display the current month name with 'date', simply do:

# echo "It is $(date '+%b')"

If you want to convert a numeric month value to the corresponding month name you could do something like this:

#. showmonth
#!/usr/bin/sh
MONTH=$1
[ -z "${MONTH}" ] && { echo "Month Number expected: '$MONTH'"; exit 1; }
MONTH=$(($MONTH-1))
set -A MONNAMES Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
echo ${MONNAMES[$MONTH]}

# ./showmonth 2
Feb

Regards!

...JRF...
Acclaimed Contributor [Founder]

Re: Find last month name

Hi (again):

Aside from checking the range of valid input values (1-12) one small change to the example script I offered might help you.

If you use my original script, but pass the month number with a leading zero just as you might if you parsed it from a longer date, like '08/08/2010', your code would fail for August and September (months 8 and 9):

./showmonth 08
./showmonth[4]: 08-1: The specified number is not valid for this command.

The shell interprets numeric values with a leading zero (0) as octal numbers. Thus, values through '07' are valid, but a one like '08' isn't.

This is easily rectified using the 'typeset' declaration with '-LZ' to left-justify and remove leading zeros.

# cat ./showmonth
#!/usr/bin/sh
typeset -LZ MONTH=$1
[ -z "${MONTH}" ] && { echo "Month Number expected: '$MONTH'"; exit 1; }
MONTH=$(($MONTH-1))
set -A MONNAMES Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
echo ${MONNAMES[$MONTH]}

# ./showmonth 08
Aug

See the 'sh-posix' manpages for more information.

Regards!

...JRF...
Honored Contributor [Founder]

Re: Find last month name

Dennis, good help

James, solution is good but not the exact one that I wanted.

I have a script which will purge the log files of the last month.
a warrior never quits
Acclaimed Contributor [Founder]

Re: Find last month name

[ Edited ]

>JRF: your code would fail for August and September (months 8 and 9):

I was thinking I might have a problem like that.

>The shell interprets numeric values with a leading zero as octal numbers.

This is broken, and C Standard is broken. A better language design to change the radix, should have used something explicit: 0o777

If you want the month names from the locale can do something like the following thread:

http://h30499.www3.hp.com/t5/Languages-and-Scripting/Get-last-months-month-in-alpha/m-p/5026703#U5026711

Regular Advisor

Re: Find last month name

I use the infamous caljd for this.

Knowing that 15 days ago is last month (especially when I run this on the first day of each month):

# caljd.sh -M -p 15 -o
Jan
Acclaimed Contributor [Founder]

Re: Find last month name

>JRF: your code would fail for August and September (months 8 and 9):

Yes, this fails for the Posix shell but not for ksh.
Acclaimed Contributor [Founder]

Re: Find last month name

Hi (again):

And in keeping with your suggestion to internationalize, We could create an array of month names thusly:

# set -A MONNAMES $(locale -k abmon|sed -e 's/^abmon=//;s/"//g;s/;/ /g')

# MONTH=3;echo ${MONNAMES[${MONTH}-1]}
Mar

Regards!

...JRF...
Occasional Visitor

Re: Find last month name

Just a thought.

curMon=$(date '+%b');
case ${curMon} in
        Jan ) prvMon="Dec" ;;
        Feb ) prvMon="Jan" ;;
        Mar ) prvMon="Feb" ;;
        Apr ) prvMon="Mar" ;;
        May ) prvMon="Apr" ;;
        Jun ) prvMon="May" ;;
        Jul ) prvMon="Jun" ;;
        Aug ) prvMon="Jul" ;;
        Sep ) prvMon="Aug" ;;
        Oct ) prvMon="Sep" ;;
        Nov ) prvMon="Oct" ;;
        Dec ) prvMon="Nov" ;;
esac

Couldn't help but play with the array version.

 

#!/usr/bin/ksh

set -A pMonths Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov ;

(( prvMonthNum=$(( $(date '+%m') - 1 ))  ));

prvMon=${pMonths[${prvMonthNum}]};