-4

Actually i have a bash script file and i want to execute it on the last thursday of every month please help me to find out the proper solution of that problem using bash scripting.

pawan
  • 1
  • 1
  • 2

2 Answers2

3

The way I would tackle this is to run the script using cron every Thursday (day 4 of the week). I would then have my script determine if today was the last Thursday of the month by examining the month of next Thursday. If it is the same as the month for today then today is not the last Thursday of the month.

The date command is quite versatile. You can get the numeric value of a month using +%m format so the value for today is

date +%m
03

The date command can do addition too so

date +%m now+7days

will at the appropriate time return the numeric vaule of next month which is different from that of today.

user9517
  • 115,471
  • 20
  • 215
  • 297
-2

Assuming you don't care about holidays, you can add entry in crontab as below. Please note it's difficult to standardise because of February, which only has 28 days. So below script is assuming Thursday of 4th week of every month.

"minute" "hour" 22-28 * * /usr/bin/test $(date +%w) -eq 4 && "Your script"

With this script, usr/bin/test $(date +%w) -eq 4 will be executed first. And only when it returns no error, it will execute your script.

shogo2022
  • 117
  • 5
  • This fails for March, June, July, September and December 2016 all of which have a last Thursday later than 28th. – user9517 Mar 01 '16 at 09:51