Operating System - HP-UX
1849254 Members
5397 Online
104042 Solutions
New Discussion

Last working day of the month?

 
SOLVED
Go to solution
David Yandry
Frequent Advisor

Last working day of the month?

Hello Gurus,

I have searched the Forums and found a solution to the "last day of the month" using A. Clay's caljd.sh. I have not found a solution to determine the last working day of the month. For example, if the month ends on a Monday through Friday then it's very easy but if the month ends during the weekend or a holiday then I want the script to run before that. The other complication is that I want to run my scripts only once per month. For example, we typically are off from Christmas Eve until New Year's. I want the scripts to run only once for December.

I haven't been able to devise a method to do this. Any ideas out there?

Thanks,
David Yandry
5 REPLIES 5
Dave La Mar
Honored Contributor

Re: Last working day of the month?

With the exection of February -
`date +%u` | read day
`date +%d` | read day_of_month

if [ $day -le 5 -a $day_of_month -ge 29 ]
then
do whatever
fi

Still prefer Clay's amny examples.

Best of luck.

Regards,

dl

"I'm not dumb. I just have a command of thoroughly useless information."
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Last working day of the month?

Well, since you already know about caljd.sh then this here ain't no hill for a climber. I would make certain that you have the latest version, 2.22 and that you /etc/acct/holidays files is current. I actually make a small "improvement" in the holidays file in that there should be an /etc/acct/holidays_YYYY that is symbolically linked to the current years /etc/acct/holidays because caljd.sh calculations might span years. The real key is to create a file that contains a line like
YYYY_MM:DD
for each month that the conditions are first satisfied. The DD is appended so that you can know what day actually triggered the command. The script itself simply searches for an entry YYYY_MM: at the beginning of a line. If that line is found then the stuff's done been did for this here month but if'fn you don't find no such entry then you ain't done did the command. In that case, do that there commmand and write me an entry in this here file.

Something like this:

------------------------------------
#!/usr/bin/sh

DTFILE="/var/spool/donedates"

typeset -i10 STAT=0
if [[ $(caljd.sh -M) -ne $(caljd.sh -n 1 -x 6 -x 0 -h -M) ]]
then # last working day of month
typeset -i10 DO_IT=1
typeset DT=$(date '+%Y_%m:')
typeset DT_TARGET="^${DT}"
if [[ -f "${DTFILE}" ]]
then
grep -q -E "${DT_TARGET}" ${DTFILE}
FNDSTAT=${?}
if [[ ${FNDSTAT} -eq 0 ]]
then
DO_IT=0
fi
fi
if [[ ${DO_IT} -ne 0 ]]
then # we have not already done it this month
# Your commands go here and set STAT = 0
# if ok
#
# Now log the event to the date file so we only do this
# once per month
if [[ ${STAT} -eq 0 ]]
then
echo "${DT}$(date '+%d')" >> ${DTFILE}
fi
fi
fi
exit ${STAT}


--------------------------------
I was typing this "on the fly" so I hope there are no typo's.

That should do it, Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Last working day of the month?

That was so ugly I'll post the script (thank God for Copy Buffers) as an attachment to make it prettier.

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Last working day of the month?

I should add that if you are running this from cron, make sure that you export and set PATH to include caljd.sh's location along with /usr/bin. I would execute the script once per day, Monday thru Fridays.

If it ain't broke, I can fix that.
David Yandry
Frequent Advisor

Re: Last working day of the month?

Thanks Clay. I have been working on this for 2 days and I'll bet that you did this in two minutes! and you made it look easy You're the man!!

Thanks,
David Yandry