print previous month (current month minus 1) with Solaris date and ksh


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers print previous month (current month minus 1) with Solaris date and ksh
# 1  
Old 05-06-2010
print previous month (current month minus 1) with Solaris date and ksh

Hi folks

Code:
month=`date +%m`

gives current month

Howto print previous month (current month minus 1) with Solaris date and ksh
# 2  
Old 05-06-2010
one way using shell only
Code:
#!/bin/ksh
last_month()
{
  set -A mon filler Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov
  typeset -i var=$(date +%m)
  echo ${mon[var]}
}

last_month

If you want a numeric month
Code:
typeset -i var=$(( date +%m -1 ))
[[ $var -eq 0 ]]  && echo 12 || echo $var

# 3  
Old 05-07-2010
Quote:
Originally Posted by jim mcnamara
[/code]If you want a numeric month
[code]
typeset -i var=$(( date +%m -1 ))
[[ $var -eq 0 ]] && echo 12 || echo $var
Hi jim mcnamara

thanks for your answer. I wish numeric month, but it doesn't work:

Code:
$ ./test.sh
./test.sh[2]:  date +%m -1 : bad number

# 4  
Old 05-07-2010
Try this...

Code:
last_month=`date --date "last month" +%m`

# 5  
Old 05-07-2010
Quote:
Originally Posted by itkamaraj
Try this...

Code:
last_month=`date --date "last month" +%m`

SOLARISdate != GNUdate
# 6  
Old 05-07-2010
More or less based on jim_mcnamara's above, but allows for implicit and explicit usage:

Code:
last_month() 
{
   typeset my_arg="${1:-$(date '+%m')}" 
   typeset digit_array='01:12 02:01 03:02 04:03 05:04 06:05 07:06 08:07 09:08 10:09 11:10 12:11 ' 
   typeset alpha_array='Jan:Dec Feb:Jan Mar:Feb Apr:Mar May:Apr Jun:May Jul:Jun Aug:Jul Sep:Aug Oct:Sep Nov:Oct Dec:Nov ' 

   mon=$(echo ${digit_array} ${alpha_array} |tr ' ' "\n" |grep -n "^0*${my_arg}" |awk -F':' '{print $3;}')

   echo ${mon} 
}

last_month 
last_month 10 
last_month Apr

These 2 Users Gave Thanks to curleb For This Post:
# 7  
Old 05-07-2010
If your ksh is actually ksh93 ....
Code:
$ printf "%(%m)T\n" "last month"
04
$

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

How to minus 2 month from current date?

I am running a script in ksh to get the 2 months back date from system date.The below code is giving correct date output from putty command prompt.But while running the script is .ksh file it is giving the error below.Please suggest. ; d=a; y=a m-=num while(m < 1) {m+=12; y--}... (1 Reply)
Discussion started by: hini
1 Replies

2. AIX

AIX - Get next month from current date

As said in object, how can i obtain that? In linux i use date -d "1 month" +"%m%Y". Thanks i advance. (8 Replies)
Discussion started by: fabfisc
8 Replies

3. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

4. UNIX for Dummies Questions & Answers

Get the previous month from date

Hi All, I am using the below code to get the year and month from date: Below gives output like 201212. dt=`date '+%Y%m'` how do i get the previous month value(ie: subtract 1 from date) example output: dt=201211 Please help. :confused: (3 Replies)
Discussion started by: abhi_123
3 Replies

5. Shell Programming and Scripting

current date - one month in AIX

Hi, i unable to get the last month date in AIX. current date - one month Based on the forums tried but did not find the relevent information. Any help grealy appriciated. Thanks Suri. (3 Replies)
Discussion started by: onesuri
3 Replies

6. UNIX for Dummies Questions & Answers

date command - getting previous month

Hi, On any given day, I want to capture the month that has gone by - said otherwise, how do I capture last month? expr date '+%m' - 1 Above expression is giving error. Please advise thanks ---------- Post updated at 09:28 AM ---------- Previous update was at 09:11 AM... (1 Reply)
Discussion started by: ab_2010
1 Replies

7. Shell Programming and Scripting

Script to find previous month last day minus one day timestamp

Hi All, I need to find the previous month last day minus one day, using shell script. Can you guys help me to do this. My Requirment is as below: Input for me will be 2000909(YYYYMM) I need the previous months last day minus 1 day timestamp. That is i need 2000908 months last day minus ... (3 Replies)
Discussion started by: girish.raos
3 Replies

8. UNIX for Dummies Questions & Answers

How to get the previous month by using `date`

Hello, I'm new to shell scripting. We've develop a script which will grep a file on the search criteria, MON (Jan/Feb/Mar/etc). But we should set this sript in cron which will run on every first day of the month. The problem I'm having is, when I run the script, it is displaying the contents of... (7 Replies)
Discussion started by: suneelj
7 Replies

9. Shell Programming and Scripting

needs to display month for previous day date

Hello, I wanted to display the month for previous day date. Like, today date is 18-Nov-2008. So the previous date is 17-Nov-2008. The output should be November. If the today date is 1-DEC-2008, then output should be NOVEMBER. If the today date is 1-JAN-2008, then output should be DECEMBER.... (4 Replies)
Discussion started by: govindts
4 Replies

10. Shell Programming and Scripting

Help, I need to get the last date of previous month

Hi, I'm new with Unix, I'm trying to get a last day of previous month with this format: %b %d %Y (example: Feb 25 2008). Here is what I have so far. #!/bin/ksh cur_month=`date +%m` cur_year=`date +%Y` prev_month=$(($cur_month-1)) # Check to see if this is January if then ... (11 Replies)
Discussion started by: sirrtuan
11 Replies
Login or Register to Ask a Question