Work the Shell - More Fun with Days and Dates
I received a very interesting note from a reader—a note that referred to a very interesting problem:
Many UNIX commands (for example, last) and log files show brain-dead date strings, such as “Thu Feb 24”. Does anybody out there have a script that will convert that to a year, given a five-year interval and defaulting to the present?
Given a day of the week, a month and a day, is it possible to calculate quickly the most recent year in the past when that particular date occurred on that day of the week? Of course it is!
Various formulas exist for calculating this sort of thing, but I realized pretty quickly that the handy cal utility can do the work for us. If you haven't experimented with it, you'll be surprised at what it can do. Here are two quick, relevant examples:
$ cal March 2011 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $ cal mar 2007 March 2007 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Any light bulb starting to glow for you? If you know the month and day, you simply can go backward looking at that month's day-of-week layout until finally you find a match.
In a rudimentary fashion, the basic idea can be illustrated with a loop, like this:
repeat cal $month $year | grep $day if day-of-week matches echo date $month $day most recently occurred in $year else year=$(( $year - 1 )) end repeat
Of course, the problem is a bit more complicated (as they always are), partially because of the complexity of calculating what day a specific date occurs in the cal output. There's another complication too, however; the requested date actually might have occurred in the current year, so it's not as simple as starting with the year 2010 and going backward.
The first task is to figure out how to get the information from the user. We'll have only three input parameters and do relatively little testing for misspelled day names and so on:
if [ $# -ne 3 ] ; then echo "Usage: $(basename $0) weekday month day" echo " (example: $(basename $0) wed aug 3 )" exit 1 fi
That's straightforward and pretty typical, offering a nice usage tip if you forget how to use the script. As is typical of scripts, we return a nonzero result upon error too.
We can't work with completely arbitrary data, however, so when we grab the first few parameters, we'll transliterate them into lowercase and chop off all but the first three letters:
weekday=$(echo $1 | tr '[[:upper:]]' '[[:lower:]]'; | cut -c1-3) month=$(echo $2 | tr '[[:upper:]]' '[[:lower:]]'; | cut -c1-3) day=$3
Given “Monday, February 8”, it'd be converted automatically to “mon” and “feb” for subsequent testing.
We also need the current date fields for testing, and to do this, I'll demonstrate a very neat trick of date that makes this incredibly efficient:
eval $(date "+thismonth=%m; thisday=%d; thisyear=%Y")
The eval function interprets its argument as if it were a direct script command. More interesting, date can output arbitrary formats (as documented in strftime if you want to read the man page) by using the + prefix, with %m the month number, %d the day of the month and %Y the year. The result is that date creates the string:
thismonth=03; thisday=01; thisyear=2011
which then is interpreted by the shell to create and instantiate the three named variables. Neat, eh?
It turns out that users can specify a month by name or number on the command line. If it's already a number, it'll survive the transforms intact. If it's a name though, we also need the number, so we can figure out whether the date specified could be earlier this year. There are several ways to do this, including a case statement, but that's a lot of work. Instead, I'll lean on sed as I quite frequently do:
monthnum=$(echo $month | sed 's/jan/1/;s/feb/2/;s/mar/3/;s/apr/4/;s/may/5/;s/jun/ ↪6/;s/jul/7/;s/aug/8/;s/sep/9/;s/oct/10/;s/ ↪nov/11/;s/dec/12/')
Here's where a misspelled month name is a problem, but that's a challenge beyond the scope of this script. For now, however, we'll just roll with it.
Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.
Practical Task Scheduling Deployment
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.View Now!
|The Firebird Project's Firebird Relational Database||Jul 29, 2016|
|Stunnel Security for Oracle||Jul 28, 2016|
|SUSE LLC's SUSE Manager||Jul 21, 2016|
|My +1 Sword of Productivity||Jul 20, 2016|
|Non-Linux FOSS: Caffeine!||Jul 19, 2016|
|Murat Yener and Onur Dundar's Expert Android Studio (Wrox)||Jul 18, 2016|
- Stunnel Security for Oracle
- The Firebird Project's Firebird Relational Database
- SUSE LLC's SUSE Manager
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- Managing Linux Using Puppet
- My +1 Sword of Productivity
- Non-Linux FOSS: Caffeine!
- Doing for User Space What We Did for Kernel Space
- SuperTuxKart 0.9.2 Released
- Google's SwiftShader Released
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide