[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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