Does anyone have an elegant solution for finding the date of the 3rd Monday each month and the date of the Sunday preceding? (Sunday shouldn't be beyond my capabilities, once the Monday part is worked out.) For the past several years, I've been handling the chore using a file with a manually input list of dates -- which is about as elegant as driving a tack with a sledge hammer.
How about something like:
YEAR=2005 MONTH=12 for ((DAY=1;DAY<=31;++DAY)); do LC_ALL=C TZ=UTC date -d "$YEAR-$MONTH-$DAY" done | grep "^Mon " | head -n 3 | tail -n 1
Possibly without the TZ=UTC depending on your needs...
Cheers MaZe?
On Sun, 4 Dec 2005, Robert wrote:
Does anyone have an elegant solution for finding the date of the 3rd Monday each month and the date of the Sunday preceding? (Sunday shouldn't be beyond my capabilities, once the Monday part is worked out.) For the past several years, I've been handling the chore using a file with a manually input list of dates -- which is about as elegant as driving a tack with a sledge hammer. _______________________________________________ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
� wrote:
How about something like:
YEAR=2005 MONTH=12 for ((DAY=1;DAY<=31;++DAY)); do LC_ALL=C TZ=UTC date -d "$YEAR-$MONTH-$DAY" done | grep "^Mon " | head -n 3 | tail -n 1
Possibly without the TZ=UTC depending on your needs...
A better and faster option is to work out what day of the week the 21st falls on (e.g. Wednesday, or 4) and count back that value (minus 1 to count for Sunday):
M=12 Y=2005 echo date +%d -d $Y/$M/$((21-$(($(date +%w -d $Y/$M/21)-1))))
Maciej Żenczykowski wrote:
On Sun, 4 Dec 2005, Robert wrote:
Does anyone have an elegant solution for finding the date of the 3rd Monday each month and the date of the Sunday preceding? (Sunday shouldn't be beyond my capabilities, once the Monday part is worked out.) For the past several years, I've been handling the chore using a file with a manually input list of dates -- which is about as elegant as driving a tack with a sledge hammer. _______________________________________________
How about something like:
YEAR=2005 MONTH=12 for ((DAY=1;DAY<=31;++DAY)); do LC_ALL=C TZ=UTC date -d "$YEAR-$MONTH-$DAY" done | grep "^Mon " | head -n 3 | tail -n 1
Possibly without the TZ=UTC depending on your needs...
Cheers MaZe?
Considering my nebulous request and your apparent lack of expertise in applied clairvoyance, fine. The "3rd Monday" part is used in generating reminders to a each of a group of OldFarts that gets together monthly to inventory aches, pains and, yes, empty chairs. The "Sunday before" requirement will be used to run a script (already working) to create a series of image files containing critical stuff, to be burned (manually) to DVDs to go with me to the meeting, to be given to a trusted person for safekeeping.
The following is what I have now, stripped down to the bare essentials for clarity(?):
[rj@mavis ~]$ crontab -l MAILTO="" * * * * * /home/rj/meetdate
[rj@mavis ~]$ cat meetdate #!/bin/bash # meetdate /home/rj/NSS.sh 10/17/2005 11/21/2005 12/19/2005 1/16/2006 2/20/2006 3/20/2006 (etc., etc., etc....) [rj@mavis ~]$
[rj@mavis ~]$ cat NSS.sh #!/bin/bash while [ $((`date -d $1 +%s` + 55800)) -le $((`date +%s`)) ] ; do shift MM=$((`date -d $1 +%s` + 55800)) # 3:30 PM on the date passed MB=$((`date -d $1 +%s` - 88200)) # 4:30 AM on the previous day # I want to match only date, hour and minute. MBS=`date -d "1/1/1970 + $MB seconds" +%D" "%H":"%M` done if [ "`date +%D" "%H":"%M`" = "$MBS" ] ; then # # Go execute backup script # # echo Finished Backup Crap else echo "Nope! Next backup will be at " $MBS echo "Meeting will be on " $1" at 3:30 PM" fi [rj@mavis ~]$
As it stands now, I have to manually edit the file "meetdate" from time to time, adding another year or two of dates. It works fine but it just ain't pretty.
Robert wrote:
Considering my nebulous request and your apparent lack of expertise in applied clairvoyance, fine. The "3rd Monday" part is used in generating reminders to a each of a group of OldFarts that gets together monthly to inventory aches, pains and, yes, empty chairs. The "Sunday before" requirement will be used to run a script (already working) to create a series of image files containing critical stuff, to be burned (manually) to DVDs to go with me to the meeting, to be given to a trusted person for safekeeping.
The following is what I have now, stripped down to the bare essentials for clarity(?):
Looking at the way you've done it, you could to it like this:
if [$(date +%d) == $(date +%d -d $(date +%Y/%m)/$((21-$(($(date +%w -d $(date +%Y/%m)/21)-1)))))]; then // do 3rd Monday stuff... elif [$(date +%d) == $(date +%d -d $(date +%Y/%m)/$((21-$(($(date +%w -d $(date +%Y/%m)/21))))))]; then // do Sunday before stuff else // do nothing :) fi
and therefore run the NSS.sh script directly.
On Sun, 2005-12-04 at 15:35, Jonathan Wright wrote:
Robert wrote:
Considering my nebulous request and your apparent lack of expertise in applied clairvoyance, fine. The "3rd Monday" part is used in generating reminders to a each of a group of OldFarts that gets together monthly to inventory aches, pains and, yes, empty chairs. The "Sunday before" requirement will be used to run a script (already working) to create a series of image files containing critical stuff, to be burned (manually) to DVDs to go with me to the meeting, to be given to a trusted person for safekeeping.
The following is what I have now, stripped down to the bare essentials for clarity(?):
Looking at the way you've done it, you could to it like this:
if [$(date +%d) == $(date +%d -d $(date +%Y/%m)/$((21-$(($(date +%w -d $(date +%Y/%m)/21)-1)))))]; then // do 3rd Monday stuff... elif [$(date +%d) == $(date +%d -d $(date +%Y/%m)/$((21-$(($(date +%w -d $(date +%Y/%m)/21))))))]; then // do Sunday before stuff else // do nothing :) fi
and therefore run the NSS.sh script directly.
My math is too fuzzy to decipher that. How about running this every Sunday from cron?
DOM=`date --date=tomorrow +%d` if [ "$DOM" -lt 15 -o "$DOM" -gt 21 ] then exit fi ..rest of script goes here...
I think that means today's %d must be between 14 and 20, but the concept is handy because the string you hand date= can be something like 'today + 2 weeks' if you need to track things that fall into next month.
Les Mikesell wrote:
On Sun, 2005-12-04 at 15:35, Jonathan Wright wrote:
Robert wrote:
Considering my nebulous request and your apparent lack of expertise in applied clairvoyance, fine. The "3rd Monday" part is used in generating reminders to a each of a group of OldFarts that gets together monthly to inventory aches, pains and, yes, empty chairs. The "Sunday before" requirement will be used to run a script (already working) to create a series of image files containing critical stuff, to be burned (manually) to DVDs to go with me to the meeting, to be given to a trusted person for safekeeping.
The following is what I have now, stripped down to the bare essentials for clarity(?):
Looking at the way you've done it, you could to it like this:
if [$(date +%d) == $(date +%d -d $(date +%Y/%m)/$((21-$(($(date +%w -d $(date +%Y/%m)/21)-1)))))]; then // do 3rd Monday stuff... elif [$(date +%d) == $(date +%d -d $(date +%Y/%m)/$((21-$(($(date +%w -d $(date +%Y/%m)/21))))))]; then // do Sunday before stuff else // do nothing :) fi
and therefore run the NSS.sh script directly.
My math is too fuzzy to decipher that. How about running this every Sunday from cron?
DOM=`date --date=tomorrow +%d` if [ "$DOM" -lt 15 -o "$DOM" -gt 21 ] then exit fi ..rest of script goes here...
I think that means today's %d must be between 14 and 20, but the concept is handy because the string you hand date= can be something like 'today + 2 weeks' if you need to track things that fall into next month.
Thanks to all for the responses. One of the really neat things about *nix is the fact that there are so many solutions to any given problem. One of the really neat things about CentOS is this list.