The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

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 05:59 AM
Use date command to find last month Cbish68 Shell Programming and Scripting 5 08-10-2007 10:32 AM
find out month from a date rudoraj UNIX for Dummies Questions & Answers 5 07-03-2007 08:21 AM
Formatting Date (adding a month) devid UNIX for Dummies Questions & Answers 4 01-18-2006 10:31 AM
how to get month last date in unix rajan_ka1 Shell Programming and Scripting 12 10-04-2005 07:20 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-19-2008
vanathi vanathi is offline
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.
  #2 (permalink)  
Old 03-19-2008
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
check if you can find a solution here
  #3 (permalink)  
Old 03-21-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
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 01:01 PM.. Reason: changed month=1 to month=12
  #4 (permalink)  
Old 03-21-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
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.
  #5 (permalink)  
Old 03-21-2008
curleb curleb is offline
Registered User
  
 

Join Date: Mar 2008
Location: Here, in my Ivory Tower...
Posts: 68
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
===
  #6 (permalink)  
Old 03-21-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
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.
  #7 (permalink)  
Old 03-21-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
And perl/python/C is better solution.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:37 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0