The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help, I need to get the last date of previous month sirrtuan Shell Programming and Scripting 11 10-14-2008 02:59 AM
Use date command to find last month Cbish68 Shell Programming and Scripting 5 08-10-2007 07:32 AM
find out month from a date rudoraj UNIX for Dummies Questions & Answers 5 07-03-2007 05:21 AM
Formatting Date (adding a month) devid UNIX for Dummies Questions & Answers 4 01-18-2006 07:31 AM
how to get month last date in unix rajan_ka1 Shell Programming and Scripting 12 10-04-2005 04:20 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-19-2008
Registered User
 

Join Date: Jan 2008
Posts: 6
last month end date

Hi,

How to get the lastmonth's end date? (have current date) through s shell script. I need to get it and pass it to a procedure.

Please advice.
Thanks in advance.
Reply With Quote
Forum Sponsor
  #2  
Old 03-19-2008
Yogesh Sawant's Avatar
Part Time Moderator and Full Time Dad
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 798
check if you can find a solution here
Reply With Quote
  #3  
Old 03-21-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
For future reference if someone reads this:
try cal
Code:
#!/bin/ksh
printf "%d %d" $(date "+%Y %m") | read year month
let month=$month-1
if [[ $month -eq 0 ]] ; then
   let year=$year-1
   let month=12
fi
cal $month $year | tr -s '\n' ' ' | awk '{print $NF}' | read day
printf "%d/%02d/%02d\n" $year $month $day

Last edited by jim mcnamara; 03-21-2008 at 10:01 AM. Reason: changed month=1 to month=12
Reply With Quote
  #4  
Old 03-21-2008
joeyg's Avatar
Moderator
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 983
Question Prior year, but month=1?

Code:
if [[ $month -eq 0 ]] ; then
   let year=$year-1
   let month=1
fi
Shouldn't this be ?
Code:
if [[ $month -eq 0 ]] ; then
   let year=$year-1
   let month=12
fi
January is month 1, so when you subtract 1 you get 0. And you rightly trap for it, decrementing the year but setting the month to 1 for January. I think you should set to 12 for December.
Reply With Quote
  #5  
Old 03-21-2008
Registered User
 

Join Date: Mar 2008
Posts: 35
Perl-based approach to deriving backwards in time...I haven't quite bothered to try going back to the future as of yet. But it also handles Leap Years...

Code:
$ pl_end_of_last_month_0=`perl -e '\
> $y= time - (86400 * (localtime(time))[3]); \
> printf "%04d%02d%02d\n", (localtime($y))[5] + 1900 ,(localtime($y))[4] + 1 ,(localtime($y))[3] ; ' ` 
$ echo $pl_end_of_last_month_0 
20070831
===
Code:
$ # Today... 
$ pl_today_0=`perl -e '\
> $y= time - (86400 * $ARGV[0]); \
> printf "%04d%02d%02d\n", (localtime($y))[5] + 1900 ,(localtime($y))[4] + 1 ,(localtime($y))[3] ; ' 0 ` 
$ echo $pl_today_0 
20070912
=== 
$ # Today minus 1... (um, yesterday...?) 
$ pl_today_1=`perl -e '\
> $y= time - (86400 * $ARGV[0]); \
> printf "%04d%02d%02d\n", (localtime($y))[5] + 1900 ,(localtime($y))[4] + 1 ,(localtime($y))[3] ; ' 1 ` 
$ echo $pl_today_1 
20070911
=== 
$ # Today minus a defined number... 
$ my_number=3 
$ pl_today_mynumber=`perl -e '\
> $y= time - (86400 * $ARGV[0]); \
> printf "%04d%02d%02d\n", (localtime($y))[5] + 1900 ,(localtime($y))[4] + 1 ,(localtime($y))[3] ; ' ${my_number} ` 
20070909
===
Reply With Quote
  #6  
Old 03-21-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
Quote:
Originally Posted by joeyg View Post
Code:
if [[ $month -eq 0 ]] ; then
   let year=$year-1
   let month=1
fi
Shouldn't this be ?
Code:
if [[ $month -eq 0 ]] ; then
   let year=$year-1
   let month=12
fi
January is month 1, so when you subtract 1 you get 0. And you rightly trap for it, decrementing the year but setting the month to 1 for January. I think you should set to 12 for December.
You are right. I was being bombed with other things when I did that... my bad.
Reply With Quote
  #7  
Old 03-21-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
And perl/python/C is better solution.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:06 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0