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:
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.