-1

I want to search the third Thursday in a range of dates using bash.

This is my code:

#!/bin/bash
start=2018-03-23
end=2018-04-22
while ! [[ $start > $end ]]; do
    echo $start
    start=$(date -d "$start+third Thursday" +%F)
done

Looks like the last part "$start+third Thursday" is not working well,

But using date alone with 'third Thursday' works:

date -d "third Thursday" +%F
user2637202
  • 113
  • 4
  • 11
  • Please, read how to [write a Minimum, Complete and Verifiable example a.k.a. MCVE](https://stackoverflow.com/help/mcve). Also, make a question or describe any problem you have not been able to solve on your own. – LMC Apr 25 '18 at 20:51
  • I have improved my initial question. – user2637202 Apr 25 '18 at 21:13
  • It works because $date is "", and therefore you have `date -d "+third Thursday" +%F`. – Grzegorz Apr 25 '18 at 21:18
  • Sorry, you are right. – user2637202 Apr 25 '18 at 21:20
  • Can anyone help me? – user2637202 Apr 26 '18 at 04:27
  • Why do you need the ``$end`` date? Aren't you just looking for the third Thursday after ``$start``? And if ``$start`` is a Thursday, do you want the Thursday in three weeks or does the current Thursday count as one Thursday? – Kokogino Apr 26 '18 at 06:04
  • Hi! I need `$end` because I want to search in a range of days, maybe its not necessay but I need the while loop. The `start` always is a Friday, so, dosent count as Thursday. – user2637202 Apr 26 '18 at 06:30
  • Possible duplicate of [How to find the difference in days between two dates?](https://stackoverflow.com/q/4946785/608639), [find and number of days range](https://stackoverflow.com/q/7339253/608639), [Bash how many days since](https://stackoverflow.com/q/22775108/608639), etc. – jww Apr 29 '18 at 21:19

1 Answers1

1

This code finds the third Thurday after a given date.

#!/bin/bash
start=2018-04-24
thursdayOfCurrentWeek=$((4-$( date +%u --date $start )))
if [[ $thursdayOfCurrentWeek -ge 0 ]]
then
    date -d "$start + $thursdayOfCurrentWeek days + 2 weeks" +%F
else
    date -d "$start + $thursdayOfCurrentWeek days + 3 weeks" +%F
fi

Let's split it up:

date +%u --date $start

This returns the weekday of $start as a number (1 = Monday, ..., 4 = Thursday).

$((4-$( date +%u --date $start )))

This calculates how many days to add to the $start date to get the Thursday of the current week.

if [[ $thursdayOfCurrentWeek -ge 0 ]]

If the date of the Thursday is today or later this week, then add only 2 weeks to the next Thursday since it's already one of the next 3 Thursdays.

date -d "$start + $thursdayOfCurrentWeek days + 2 weeks" +%F
# or
date -d "$start + $thursdayOfCurrentWeek days + 3 weeks" +%F

Add to or subtract off the $start date the days to get the current week's Thursday and add 2 or 3 weeks depending on when the Thursday of this week is.

If the $start date is a Thursday and it shouldn't count as one of the three just replace the if statement by:

if [[ $thursdayOfCurrentWeek -gt 0 ]]

This is a more general answer (because the start date doesn't have to be a Friday) then what you're looking for, but it also works for your use case.

Hope this helps.

PS You don't need the $end date.

EDIT:

If you really want the minimal version if the $start is always Friday you can do this:

#!/bin/bash
start=2018-03-23
date -d "$start - 1 day + 3 weeks" +%F
Kokogino
  • 984
  • 1
  • 5
  • 17