Work the Shell - More Fun with Days and Dates
The next set of tests is one I rewrote a couple times to ensure that I wasn't tripping myself up, because my first thought simply was to use a test like this:
if [ $month -le $thismonth -a $day -le $thisday ]
But, then I realized that in edge cases it wouldn't actually work properly. For example, let's say it's April 4 and you're checking for March 11. The month test succeeds, but the day test fails—not what we want. Instead, let's use a cascading set of conditional tests:
if [ $monthnum -gt $thismonth ] ; then # month is in the future, can't be this year mostrecent=$(( $thisyear - 1 )) elif [ $monthnum -eq $thismonth -a $day -gt $thisday ] ; then # right month, but seeking a date in the future mostrecent=$(( $thisyear - 1 )) else mostrecent=$thisyear fi
With just this much code, we can at least test the normalization of data input and comparison tool. I ran this set of tests on March 1, by the way:
$ whatyear.sh Monday Aug 3 Decided that for 8/3 we're looking at year 2010 $ sh whatyear.sh mon jan 9 Decided that for 1/9 we're looking at year 2011 $ whatyear.sh mon mar 1 Decided that for 3/1 we're looking at year 2011 $ whatyear.sh mon mar 2 Decided that for 3/2 we're looking at year 2010
It correctly identified that the current date could be a match, but that the subsequent day (mar 2) had to be in the previous year for it to be a possibility.
Good. Next month, we'll put the rest of the LEGO pieces in the model and have a working script. The big task left? Parsing the output of cal to figure out the day of the week for a given date.
Dave Taylor has been hacking shell scripts for a really long time, 30 years. 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.
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.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- Designing Electronics with Linux
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Build a Skype Server for Your Home Phone System
- New Products
- Why Python?
- A Topic for Discussion - Open Source Feature-Richness?
- Validate an E-Mail Address with PHP, the Right Way
- Tech Tip: Really Simple HTTP Server with Python
- Understanding the Linux Kernel
1 hour 47 min ago - General
4 hours 17 min ago - Kernel Problem
14 hours 20 min ago - BASH script to log IPs on public web server
18 hours 47 min ago - DynDNS
22 hours 23 min ago - Reply to comment | Linux Journal
22 hours 55 min ago - All the articles you talked
1 day 1 hour ago - All the articles you talked
1 day 1 hour ago - All the articles you talked
1 day 1 hour ago - myip
1 day 5 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
GNU `date' is enough...
As we are talking about Linux, the GNU `date' utility could be far more clever than you might thought, so there is no need to mess with the output of `cal'...
Quick hint:
date -d "Sep 25 2008" +%Adate -d "Sep 25 2008" +%uBut this probably is GNU only, AFAIK, at least the BSD `date' does not have such magic.
Here is an `sh' script listing I just crafted:
#!/bin/sh export LC_TIME=C usage() { cat <<! USAGE: ${0##*/} WEEKDAY MONTH_NAME Day ${0##*/} WEEKDAY MONTH-DAY ${0##*/} WEEKDAY MONTH/DAY ! } if [ $# -ne 3 -a $# -ne 2 ]; then usage exit 1 fi if [ $# -eq 3 ]; then # GNU `date' accepts "Sep 25 2008" fmt="$2 $3 %d" else # And also accepts "2011-9-25" or "9/25/2011" case "$2" in *-*) fmt="%d-$2" ;; */*) fmt="$2/%d" ;; *) echo "Uknown date: $2" usage exit 1 ;; esac fi case $(echo $1 | tr '[:upper:]' '[:lower:]' | cut -c1-3) in mon) weekday=1 ;; tue) weekday=2 ;; wed) weekday=3 ;; thu) weekday=4 ;; fri) weekday=5 ;; sat) weekday=6 ;; sun) weekday=7 ;; *) echo "$1: Unknown weekday" exit 1 ;; esac MY_DATE_FMT="%Y/%m/%d" MY_WDAY_FMT="%A" MAX_TRY=5 y0=$(date +%Y) i=0 found=0 while [ $i -lt $MAX_TRY ]; do y=$((y0 - i)) str=$(printf "$fmt" $y) if ! j=$(date -d "$str" +%u); then # `date' will complain, so I keep quiet exit 1 fi if [ $j -eq $weekday ]; then echo $(date -d "$str" +$MY_DATE_FMT) is \ $(date -d "$str" +$MY_WDAY_FMT) found=1 fi i=$((i + 1)) done test $found -eq 0 && exit 1 exit 0