Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get the Previous month in Korn Shell

1,593 views
Skip to first unread message

narendra vuradi

unread,
Mar 18, 2008, 11:23:13 PM3/18/08
to nvu...@gmail.com
Hi I have a requirement where i have to append the prev month (e.g Feb
for current date ) to the file name before sending it for some other
processing.

if input file name is
xyz.txt output for the today's run should be xyz_Feb.txt.

i tried this code
1 curmth=`date +%m`
2 mth=(Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
3 prevmth=${mth[$((curmth - 1))]}
4 echo $prevmth

but its giving a syntax error at Line 2 : `(' unexpected.

TIA.

Regards,
naren

Message has been deleted

John W. Krahn

unread,
Mar 18, 2008, 11:49:19 PM3/18/08
to
narendra vuradi wrote:
> Hi I have a requirement where i have to append the prev month (e.g Feb
> for current date ) to the file name before sending it for some other
> processing.
>
> if input file name is
> xyz.txt output for the today's run should be xyz_Feb.txt.

$ date '+%b'
Mar
$ date -d 'last month' '+%b'
Feb

John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Message has been deleted

narendra vuradi

unread,
Mar 19, 2008, 12:17:48 AM3/19/08
to

hi when i ran the above code i am getting the following error
date: illegal option -- d
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]

Chris F.A. Johnson

unread,
Mar 19, 2008, 12:53:27 AM3/19/08
to
On 2008-03-19, narendra vuradi wrote:
> Hi I have a requirement where i have to append the prev month (e.g Feb
> for current date ) to the file name before sending it for some other
> processing.

If you have the GNU version of date:

date -d 'last month' +%b

Or try the date arithmetic section of the FAQ:
<http://cfaj.freeshell.org/shell/cus-faq.html#6>

Also, Chapter 8 of my book, which is online at
<http://cfaj.freeshell.org/shell/ssr/08-The-Dating-Game.shtml>

> if input file name is
> xyz.txt output for the today's run should be xyz_Feb.txt.
>
> i tried this code
> 1 curmth=`date +%m`
> 2 mth=(Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
> 3 prevmth=${mth[$((curmth - 1))]}
> 4 echo $prevmth
>
> but its giving a syntax error at Line 2 : `(' unexpected.

The shell you are using doesn't have arrays (they are not
standard).

curmth=`date +%b`
mth=DecNovOctSepAugJulJunMayAprMarFebJanDec
tmp=${mth#*$curmth}
prevmth=${tmp%${tmp#???}}

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Rakesh Sharma

unread,
Mar 19, 2008, 2:42:11 AM3/19/08
to

set 'Dec' 'Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct'
'Nov'
set x `expr \`date '+%m'\` + 1` ${1+"$@"}; shift
eval "prevmth=\${$1}"

Stephane CHAZELAS

unread,
Mar 19, 2008, 4:03:33 AM3/19/08
to
2008-03-18, 20:23(-07), narendra vuradi:
[...]

IFS=';'
set -f
set -- $(locale abmon)
set x "${12}" "$@"
now=$(date +%m)
shift "${now#0}"
lastmonth=$1


--
St�phane

0 new messages