• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

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
0
mngong_rc
Asked:
mngong_rc
  • 4
  • 2
1 Solution
 
woolmilkporcCommented:
If you have GNU date:

date -d "1 month ago" +%B

wmp
0
 
woolmilkporcCommented:
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 ...

0
 
woolmilkporcCommented:
... 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))]}

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
mngong_rcAuthor Commented:
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
0
 
woolmilkporcCommented:
Looks like ksh!

If it's AIX you can use ksh and do

#!/bin/ksh
set -A M Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
...


0
 
mngong_rcAuthor Commented:
Found another way out but will be giving points to woolmilkporc
Looks like it should have been beefMilkporc
Thanks
MNT
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now