LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-23-2014, 05:18 PM   #1
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Rep: Reputation: 29
friday before the last Tuesday of the month -- script


I need a script that 'cron' can read and if it is the Friday before the last Tuesday of each month, call another script that will send an email.

I know how to set up 'cron'. I have the emailing code, all I need it how to figure out the date.

I have done this before using BRUTE FORCE simuler to this faux code example.
Code:
read date
if month == 'September' && day == '22'; then send = True
	elif
		.......
	elif
		.......
	elif
		.......
This will need to be updated each year. Is there an algorithm that will calculate this. So I can put this function in a file and not have to worry about it for 10 years.

Where should I go, or what should I Google for information on this subject?
 
Old 09-23-2014, 05:29 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
What if you do something along the lines of
Code:
if (today == 'Friday') &&
   month(today) == month(today+4 days) &&
   month(today) != month(today+11 days)
do stuff
Pseudocode obviously, but the logic should be sound and it should be very easy to implement with date's format code and -d flag.

today == 'Friday' ensures that today is Friday (obviously)
month(today) == month(today+4) ensures that the following Tuesday is still part of the same month
month(today) != month(today+11) ensures that the following Tuesday is the last Tuesday in the month


edit: something along the lines of
Code:
thisday=$(date +%u)
thismon=$(date +%m)
thisp4mon=$(date -d "+4 days" +%m)
thisp11mon=$(date -d "+11 days" +%m)

if [[ $thisday -eq 5 && $thismon -eq $thisp4mon && $thismon -ne $thisp11mon ]]; then
   echo "This is the Friday before the last Tuesday of the month!"
fi

Last edited by suicidaleggroll; 09-23-2014 at 05:38 PM.
 
2 members found this post helpful.
Old 09-23-2014, 07:11 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,749

Rep: Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928
It would be easier to determine the last Tues of the month and subtract 4. You can find many examples of finding a certain day of the week but set the cron job to run the script on Tuesdays and then check the day of the month for >24 (I think this is correct). If true then it is the last Tues and in your script subtract 4 days instead of adding as above.

[ date "+%d" -gt 24 ] && /path/to/your/script

Not necessary to adjust for years...

Last edited by michaelk; 09-23-2014 at 07:13 PM.
 
Old 09-23-2014, 07:38 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
My interpretation of the problem is not that he needs to know what date the Friday before the last Tuesday of the month is, but that he needs to run the script on the Friday before the last Tuesday of the month. So checking whether "today" is the last Tuesday of the month doesn't really help, it just tells you that you should have run the script four days ago. Maybe I misunderstood the question.

You won't be able to check for date < 24 or any other fixed number, since the month could have 28, 29, 30, or 31 days. To take that approach, you would need to check the current month and reference a lookup table with the number of days in each month, in addition to checking whether or not it's a leap year if the month is February. It's doable, but seems much more complicated to me.
 
Old 09-23-2014, 08:18 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,749

Rep: Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928
Doh. your correct.
 
Old 09-24-2014, 12:12 AM   #6
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Original Poster
Rep: Reputation: 29
Thank you for the input and feed back. I will try it this coming Friday and report back.

Dave
 
Old 09-25-2014, 12:03 AM   #7
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
cal mm yyyy pipe to awk and derive (by comparison in each record) the max value of column 6 (friday). on END subtract 4 from it and send the value where you want it.

While testing, I added two selection conditions and the code becomes:
Code:
cal 09 2014 | awk 'NR>2 && NF>5 {if {$6>m) m=$6;} END(print m-3}'
I also tested it with arguments 10 2014 and 11 2014 to check for the end conditions and it worked fine.

OOPS sorry.
I just realized that I did "Tuesday before the last Friday". But the idea is OK. The code for "Friday before last Tuesday" becomes:
Code:
cal 09 2014 | awk 'NR>2 && NF>1 {if {$2>m) m=$2;} END(print m-4}'
To be tested.

OK

Last edited by AnanthaP; 09-27-2014 at 01:08 AM.
 
Old 09-25-2014, 12:22 PM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,783

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
Here is a script that will reliably print the date of the Friday preceding the last Tuesday of the current month. Hopefully the verbose variable names make it understandable. Run it with "bash -x ..." if it's still not clear.
Code:
#!/bin/bash
Tues=2
First_of_this_month=$(date +%Y-%m-1)
Next_month_begins_on=$(date -d "$First_of_this_month + 1 month" +%w)
Adj=$((Tues - Next_month_begins_on))
[ $Adj -gt -1 ] && Adj=$((Adj - 7))
FinalTue=$(date -d "$First_of_this_month + 1 month $Adj day" +%F)
PreviousFriday=$(date -d "$FinalTue - 4 days" +%F)

echo $PreviousFriday

Last edited by rknichols; 09-25-2014 at 12:24 PM.
 
1 members found this post helpful.
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find/display out last Friday's date of the month sunnysthakur Linux - General 11 03-29-2013 03:09 AM
cronjob to run a command at first friday of each month a9b8c2 Linux - General 5 10-19-2012 08:24 AM
dcron - run script last friday of month cotton213 Linux - Software 7 06-25-2012 04:03 PM
Cron Job to run on 2nd and 4th Friday of each month rust8y Linux - General 1 06-25-2007 09:07 PM
howto start a perl script only from tuesday to friday at 8.00 am ? cccc Linux - Newbie 4 03-19-2005 04:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration