Last Sunday of the Month

Michael Velez mikev777 at hotmail.com
Tue Apr 5 11:59:17 UTC 2005


 

> -----Original Message-----
> From: redhat-list-bounces at redhat.com 
> [mailto:redhat-list-bounces at redhat.com] On Behalf Of Pete Nesbitt
> Sent: Tuesday, April 05, 2005 12:01 AM
> To: General Red Hat Linux discussion list
> Subject: Re: Last Sunday of the Month
> 
> On Monday 04 April 2005 12:55 pm, Steve Buehler wrote:
> > RHEL ES 3
> > I can't seem to find this about the date command, but that doesn't 
> > mean it can't do it.  I have overlooked things in the past.
> >
> > 1.  Can the date command give you a date for the Last Sunday of a 
> > month?  Or do I have to go through a loop to check each day of the 
> > month to see if it is the last Sunday of the month?
> >
> > 2.  I can do a date --date="last sunday".  Is there a way 
> to tell it 
> > to give me the date of 4 Sundays ago?  I tried date 
> --date="4 sundays 
> > ago" and that didn't do it.
> >
> > Thanks
> > Steve
> 
> 
> Hi,
> You can use -d to request a date by string, but i think it 
> only uses 'next' or 'last'.
> date -d "last month"
> Fri Mar  4 21:00:09 PST 2005
> 
> date -d "next sunday"
> Sun Apr 10 00:00:00 PDT 2005
> 
> You could check if next sunday is the current month:
> date -d "next sunday"|awk ' {print $2} ...does it = $thismonth?
> That would be a lot easier to check than checking every day.
> 
> --

Yes, check if next sunday is the current month but get the date like this:

LASTSUNDAY=`date -d "last sunday"`
NEXTSUNDAY=`date -d "next sunday"`
NEXTSUNDAYMONTH=`date -d "$NEXTSUNDAY" | awk '{print $2}'`

That will allow you to iterate in a while loop with the following (i.e. if
NEXTSUNDAYMONTH is not equal to the current month):

LASTSUNDAY=$NEXTSUNDAY
NEXTSUNDAY=`date -d "$NEXTSUNDAY + 1 week"`      # I guess this line is what
you're looking for
NEXTSUNDAYMONTH=`date -d "$NEXTSUNDAY" | awk '{print $2}'`


When NEXTSUNDAYMONTH is no longer equal to the current month, LASTSUNDAY
will contain the date of the last Sunday of the current month.  

Hope this helps,
Michael






More information about the redhat-list mailing list