Unix Tip: Last workday of the month: A retake

March 9, 2007, 04:22 PM —  ITworld.com — 

Send in your Unix questions today! |
See additional Unix tips and tricks



One of my articles last month described an approach to determining whether some particular weekday happens to be the last workday of the month. While no one took me up on the challenge to turn this script into a densely packed on-liner, one reader offered a version that has a particularly interesting advantage. Instead of being implemented as code which can be inserted into any script in which you want to execute certain commands only on the last workday of the month, this script is a standalone that you would use to invoke other scripts or specific commands when it's the appropriate time for them to run -- when it's the last workday of the month.


#!/usr/bin/env bash
# lastworkday: run a command if it is the last workday of the
# month, i.e.:
# if it's Monday-Thursday and the following days is the 1st or
# if it's Friday and the following Monday is the 1st, 2nd or 3rd

case "$#" in
  0) echo "usage: $0 program [args]"; exit 1;;
  *) ;;
esac

timezone=`date +%Z`							# time zone
DoW=`date +%w`								# day of week

tomorrow=`TZ=$timezone-24 date +%d`			# today's date (e.g., 27)
aft3days=`TZ=$timezone-72 date +%d`			# 3 days after tomorrow

case $DoW in
  [1-4]) test "$tomorrow" == 1  && lastworkday=true;;
  5)     test "$aft3days" -le 3 && lastworkday=true;;
esac

test $lastworkday && exec ${1+"$@"}			# run args IFF last workday
exit 0

The shebang line in this reworking of the script probably looks a little different than those you're used to seeing. The env command invokes the environment for bash, much as /usr/bin/bash would. However, this syntax avoids problems if the path to bash on your system is not /bin/bash.

The usage statement reminds users that they are expected to supply a command along with optional arguments.

The timezone and DoW (day of week) variables are the same as in the original script. We also calculate tomorrow's date in the same manner. We also calculate the date three days after the following date. We will need this information if today turns out to be a Friday.

In the following case statement, we use the test command to determine whether tomorrow is the first of the month (and, thus, today is the last day of the month) if today is Monday through Thursday or, if today is Friday, whether three days from tomorrow (the following Monday) is the 1st, 2nd or 3rd. If either of these conditions is true, today is the last workday of the month and we set the lastworkday variable to true.

In the final test command, we determine whether lastworkday was set to true and, if so, we run the command provided as an argument to this script, including any parameters that were supplied with the "exec ${1+"@"} command. In this syntax, $1 is the name of the command we want to run and $@ the arguments. "IFF" means "if and only if"; it's a programmer's shorthand.



The final "exit 0", indicating a successful script termination will only be executed if the preceding exec is successful.


You would run this script with a command such as:


# lastworkday prepNewLogs

You can test its logic by using the script to run a simple command
such as this:


# lastworkday cal 3 2010

The March 2010 calendar should only appear if you run this script on the last workday of the month.


Thanks to Karl Vogel for his suggestions for this script.

 

ITworld.com

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
Free books

Essential JavaFX
Get started building rich Web apps quickly with an introduction to the power of JavaFX key features -- scene node graphs, nodes as components, the coordinate system, layout options, colors and gradients, custom classes with inheritance, animation, binding, and event handlers.Enter now!

The Nomadic Developer
Consulting can be hugely rewarding, but it's easy to fail if you are unprepared. To succeed, you need a mentor who knows the lay of the land. Aaron Erickson is your mentor, and this is your guidebook. Enter now!

Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace