Link to home
Start Free TrialLog in
Avatar of mngong_rc
mngong_rc

asked on

Unix Way to get last months value from this month's date command

This month is October. Would like to have Sept
from the date command
Output of date command
Mon Oct 17 13:58:47 .
Can utilize awk to get oct and was wondering if there is a short way
to get to last months value


Thanks

MNT
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

If you have GNU date:

date -d "1 month ago" +%B

wmp
If you don't have GNU date:

#!/bin/bash
typeset -a M=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
echo ${M[$(($(date +%m)-2))]}


Not really elegant ...

... and will not work in January.

So rather try this

#!/bin/bash
typeset -a M=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
N=$(date +%m)
[ $N -eq 1 ] && N=13
echo ${M[$(($N-2))]}

Avatar of mngong_rc
mngong_rc

ASKER

Cannot tell why but this does not run on AIX


#!/usr/bin/bash
typeset -a M=(Jtestdate.ksh[2]: 0403-057 Syntax error at line 2 : `(' is not expected.




Thanks

MNT
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Found another way out but will be giving points to woolmilkporc
Looks like it should have been beefMilkporc
Thanks
MNT