- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Find last month name
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2010 03:45 AM
02-07-2010 03:45 AM
Help me to find put name of the last month.
i.e. Jan, Feb, Apr format
Solved! Go to Solution.
- Tags:
- date arithmetic
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2010 04:03 AM
02-07-2010 04:03 AM
Re: Find last month name
Please help me to find out the name of last month in shell script. (in POSIX)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2010 06:39 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2010 06:51 AM
02-07-2010 06:51 AM
Re: Find last month name
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2010 08:15 AM
02-07-2010 08:15 AM
Re: Find last month name
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2010 07:50 PM
02-07-2010 07:50 PM
Re: Find last month name
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2010 12:05 AM - edited 09-28-2012 07:40 PM
02-08-2010 12:05 AM - edited 09-28-2012 07:40 PM
Re: Find last month name
>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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2010 12:09 PM
02-08-2010 12:09 PM
Re: Find last month name
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
- Tags:
- caljd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2010 06:55 PM
02-08-2010 06:55 PM
Re: Find last month name
Yes, this fails for the Posix shell but not for ksh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2010 05:03 AM
02-09-2010 05:03 AM
Re: Find last month name
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2012 02:38 PM
09-28-2012 02:38 PM
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}]};