open.itworld.com
  Search  
Security Home Page Security Webcasts Security White Papers Security Newsletters Security News Open Topics Careers ITworld Voices ITwhirled The Security site of ITworld.com

Unix Tip: Last workday of the month: A retake

ITworld.com 3/9/2007

Sandra Henry-Stocker, 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.

On this topic

 

Sandra Henry-Stocker has been administering Unix systems for more than 18 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She currently works for TeleCommunication Systems, a wireless communications company, in Annapolis, Maryland, where no one else necessarily shares any of her opinions. She lives with her second family on a small farm on Maryland's Eastern Shore. Send comments and suggestions to bugfarm@gmail.com.




Sponsored Links

Sign up for a Microsoft Dynamics® CRM WEBCAST
Hear globally recognized leaders in customer strategy discuss the importance and evolution of CRM.
TRY NEW SUN SERVERS FREE for 60 Days!
Test The Latest Sun Servers In Your Environment BEFORE YOU BUY. Pay Nothing, Not Even Shipping.
Workflow Enabled Help Desk & IT Service Management
Automate service desk activities and integrate processes across IT. Learn more here.
Sign up for a FREE NETWORK RISK ASSESSMENT!
MORE THAN 70% OF NETWORKS ARE INFECTED by hidden Malware. Find out if your network is infected now!
Enterprise IP Goes Mobile
To maximize full productivity, companies must integrate their mobile applications with the IP network.
» Buy a link now

Advertisements
Sponsored links
Top 5 Reasons to Combine App Performance and Security
Locate Hidden Software on business PCs with this free tool
KODAK i1400 Series Scanners stand up to the challenge
Bring harmony to your mix of UNIX-Linux-Windows computing environments
 Home   Open source  Operating systems  Unix
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   IDG Connect   IDG World Expo   Infoworld   ITworld   JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.