Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding which week of the current Month 1

Status
Not open for further replies.

naveenrt

Technical User
Mar 23, 2001
53
US
Hello
I want to run my Backup every Second and Fourth Friday of the month .Is there anyway to find which week is today

For example in May , May 17 will be the Fourth week of thye month and May 1 the first week

Thanks in advance

Naveen
 
What about taking "date +%U" for getting the week of the year and just write a script to trigger your backup every 2n week?

ie. something like:

WEEKS=`date +%U` # actual number of week in the year
EVEN=`expr $WEEKS % 2` # divide it through 2 and see if there is a rest

# Unix day 5 is friday (date +%w)

if [[ $EVEN -eq 0 && `date +%w` -eq 5 ]]; then
echo "Backup job is starting..."
else
echo "Nothing to do...zzz"
fi

laters
zaxxon
 
I think that will run the backup every other week, not the 2nd and 4th week of a month... Since there is no way of telling what week you are in in the current month, we use to create a date file and then check the date against it..
So you can set up the backup to run in crontab every Friday.
In the script check the date:

DATE=`date +%m%d%y`
if [`grep -qx $DATE RUNDATE.lst` -eq 0 ]
then
run backup
else
don't run backup
fi

This is sloppy and you will have to manually set it up for a few years.... But it will work....

Love to see if anyone has a solution....
 
Hi Guys,

The following command will give you the two dates you are after. (ie. 2nd and 4th Friday of the current month).

cal | tail +3 | cut -c21-22 | grep "[0-9]" | sed -n '2p;4p'

You can change the day from friday to another by tweaking the cut command, or change the week in the month by tweaking the sed command.

Setup a script to exec via cron every friday, with something like the following verify function to prevent actually executing on the wrong days.

Today=`date +%e`
RunDays=`cal | tail +3 | cut -c21-22 | grep "[0-9]" | sed -n '2p;4p'`
if [[ `echo "$RunDays" | grep $Today` -ne $Today ]]
then # Exit Cleanly
exit 0
fi
# Now running rest of script seeing as its either 1st or 4th Friday of this month.

Hope the above is of some use to you :)

Best Regards to all.

____________________
Sometimes it pays to stay in bed on Monday, rather than spending the rest of the week debuging Mondays code.
 
d3Vzero
Great and Simple Script . This script works for all months. Thanks a lot in helping me out .

The beauty of this script is that it can be customized for any day ( Monday thursday ...etc)

Hats off to you for thinking so simply . I went in to arithmetics to solve this ... wheras you have solved the same with simple Logic

Thanks Again
Naveen
 
Cheers Ken .... :)

____________________
Sometimes it pays to stay in bed on Monday, rather than spending the rest of the week debuging Mondays code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top