Scripting GNU in the 21st Century
Most people tend to encounter shell scripts as attempts to write portable utilities that run properly on most, if not all, UNIX workalikes. Instead of making the best use of the shell and related programs, these scripts restrict themselves to the absolute most portable baseline of features. The extreme example of this sort of programming can be seen when one looks at the configure scripts generated by the autoconf program.
But this is 2004, and the full GNU environment is now commonplace. The advanced shells and utilities now are the default user environment for GNU/Linux systems, and they are available as install options on BSD-based systems. Even proprietary UNIXes often have complete sets of GNU software laid atop them to bring them up to date with the modern world. Because GNU software can be obtained at little or no cost, there is no excuse to continue scripting in a retrograde proprietary environment.
I live in the San Francisco Bay Area, mere walking distance from one of the Bay Area Rapid Transit (BART) stations. I do not drive, so I rely on the system for my trips downtown. The BART Web site offers an on-line trip planner, but I perform the same sort of query often and find the interface less convenient than a command-line script.
In order to save time, I decided to write a shell script that would fetch the train arrival information for my station and display it in a colored ASCII table on stdout. It should accept station codes for any arbitrary trip but use defaults specified in a per-user configuration file. I did not want to write the schedule analysis code, so I decided to perform a screen scrape of the BART trip planner. wget would submit the trip planner form, and the resulting Web page would be formatted with various tools.
The first line of most shell scripts begins with #!/bin/sh, which causes the script to be interpreted by the venerable old Bourne shell. This is used largely because classic Bourne is the only shell guaranteed to be on all UNIX and UNIX-like systems. Because this script is designed to work in a modern GNU system, it begins with #!/bin/bash. Users of BSD systems may wish to change it to #!/usr/local/bin/bash or perhaps #!/usr/bin/env bash.
Using bash instead of the classic Bourne shell provides us with some useful features we can put to good use in a moment. For one thing, bash allows us to break down our script using functions. The bash string manipulation routines also can save us some time by performing operations in-line that otherwise would have to be fed into an external sed or awk process.
Any good program has configuration files, so we can set up a traditional rc filesystem. The rc at the end of many configuration files stands for run commands, and it typically refers to the fact that the configuration file is loaded in like a script.
test -r /etc/bartrc && source /etc/bartrc test -r ~/.bartrc && source ~/.bartrc
We also should set the default departure and arrival station codes to Rockridge and Embarcadero, respectively. We use the compact bash syntax for an alternate value if a variable is undefined, so users can set the BARTSTART and BARTDEST variables in their own environments if they like.
BARTSTART=${BARTSTART:-ROCKR}
BARTDEST=${BARTDEST:-EMBAR}
The first function we write is the basic usage guideline message, which helps guide development of the rest of the program.
function usage {
echo "Usage:"
echo " $(basename $0) [-hl] [ [<source>] <destination> ]"
echo " To change defaults, set the BARTSTART and BARTDEST"
echo " variables in your ~/.bartrc"
echo
echo "Flags:"
echo " -l, --list List station codes with names"
echo " -h, --help This message"
}
We now have a simple usage command available that prints out the argument format for the script. Notice that we used $(basename $0) to determine automatically the filename of the script. We also allow an optional destination station code as an argument, which may be preceded by an optional departure station code.
HTTP has two methods for submitting selections to a form, GET and POST. The POST method is the most powerful, but the GET method allows us to specify values in the URL itself. This makes the GET method most convenient for scripting, because we can specify all relevant form fields as an argument to a simple tool, such as wget.
First, we set up the base URL to the form, specifying options to minimize the amount of formatting around the data.
baseurl="http://bart.gov/textonly/stations/schedule.asp?ct=1&format=quick&print=yes"
Looking at the form's HTML source code, we determine which fields have which names and begin to construct additions to the above URL. The date we're interested in is the current moment, and we use the date command's own formatting options to construct the date and time portion of the form.
date_now=$(date +"&time_mode=departs&depart_month=%m&depart_date=%d&depart_time=%I:%M+%p")
The $( ... ) syntax is simply a more explicit version of the backticks, allowing us to use the output of a command as part of a line of shell code.
Next, we use the BARTSTART and BARTDEST variables to enter the stations in which we are interested.
stations="&origin=${BARTSTART}&destination=${BARTDEST}"
Then, we use the wget utility to submit the form, redirecting all warning messages to /dev/null so as not to confuse our script. The full function looks like this:
function submitform {
baseurl="http://bart.gov/textonly/stations/schedule.asp?ct=1&format=quick&print=yes"
date_now=$(date +"&time_mode=departs&depart_month=%m&depart_date=%d&depart_time=%I:%M+%p")
stations="&origin=${BARTSTART}&destination=${BARTDEST}"
wget -O - -o /dev/null ${baseurl}${date_now}${stations}
}
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 |
- Linux Systems Administrator
- New Products
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Have you tried Boxen? It's a
1 hour 40 min ago - seo services in india
6 hours 12 min ago - For KDE install kio-mtp
6 hours 12 min ago - Evernote is much more...
8 hours 12 min ago - Reply to comment | Linux Journal
16 hours 58 min ago - Dynamic DNS
17 hours 32 min ago - Reply to comment | Linux Journal
18 hours 30 min ago - Reply to comment | Linux Journal
19 hours 21 min ago - Not free anymore
23 hours 22 min ago - Great
1 day 3 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!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
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
Re: Scripting GNU in the 21st Century
It should be noted that the LSB is POSIX-based.
Re: Scripting GNU in the 21st Century
Overall, for someone learning bash, this is probably a reasonable example. I have many similar ones myself (grabbing satellite wildfeed data, for example).
However, as a means of introducing a newcomer to bash, or as a convincing description of why the newcomer should be using bash, I feel it falls short.
For example, some simple timing shows that his "$(basename $0)" construct is almost 100x slower than using "${0##*/}", although he does use another version of the same construct later, meaing that he is aware of it!
His repeated use of the backslash character as a line continuation does not improve the readability of the script; in fact, it makes it worse. Leave it out! Yes, it still works -- if the line ends with a character which indicates there's more needed, the line continuation character is redundant. The pipe symbol (vertical bar) is such a character.
In general, all variable usage should be enclosed in double quotes (ie, "Dollar signs in Double quotes"). This technique is only wrong 1 time out of 100, so the programmer will be correct 99% of the time. :) Yes, it may mean the double quotes are redundant in some cases, but there's a lot to be said for consistency, and hence, readability. Only when dealing with word splitting (where you want the word split), will the double quotes be incorrect.
He appears to use brackets in "if" statements when the POSIX (?) technique would be double parentheses (brackets are for string comparisons, parens are for numeric comparisons, and the doubled form of each is recommended since they turn off I/O redirection, wildcarding, and word splitting). Maybe he doesn't know?
Lastly, the "liststations" function seems overly complex to me. First, is it really necessary to specify the entire XPath all the way from the "html" element down to the "select" element and its attribute?? I haven't seen the data, but I'd be willing to bet that just the "select" element and attribute would be enough (since select is non-functional outside of forms anyway, and the attribute specifies the name of the select element!). Regardless of whether simplification is possible, change the delimiter of the regexpr! Use something other than a slash and avoid LTS ("leaning toothpick syndrome", per Larry Wall). With the text thus cleaned up visually, maybe let sed also do the elimination of text up to and including the equals sign? That eliminates the need for cut, although it may hurt readability. Additionally, sed can also replace the while loop; tell sed to match on the "select" element and attribute, then read three more lines into the holding space, appending them to whats already there. Now run a substitution on the hold space and print the result. (Or if you're not comfortable with sed, use awk, and you still eliminate the cut and while loop.) YMMV.
Overall, I agree with the other comment posted here that the standard for scripts should be POSIX, not a particular tool. Of course, POSIX has its own problems (quite a few, actually!), but that's a decision that individual organizations need to make: portability vs. speed/usability.
The sh POSIX standard
The sha-bang ( #!) at the head of a script tells your system that this file is a set of commands to be fed to the command interpreter indicated. The #! is actually a two-byte [1] "magic number", a special marker that designates a file type, or in this case an executable shell script (see man magic for more details on this fascinating topic). Immediately following the sha-bang is a path name. This is the path to the program that interprets the commands in the script, whether it be a shell, a programming language, or a utility. This command interpreter then executes the commands in the script, starting at the top (line 1 of the script), ignoring comments. [2]
#!/bin/sh
#!/bin/bash
#!/usr/bin/perl
#!/usr/bin/tcl
#!/bin/sed -f
#!/usr/awk -f
Each of the above script header lines calls a different command interpreter, be it /bin/sh, the default shell (bash in a Linux system) or otherwise. [3] Using #!/bin/sh, the default Bourne Shell in most commercial variants of Unix, makes the script portable to non-Linux machines, though you may have to sacrifice a few Bash-specific features. The script will, however, conform to the POSIX [4] sh standard.
REF:
1) Advanced Bash-Scripting Guide
2) The Single UNIX Specification, Version 3
The Horror
While it's a neat hack to parse HTML using bash, and I respect the authors significant contributions to Free Software (LNX-BBC, GAR - "We're not worthy!"), isn't this really a sign that scripting activities on GNU/Linux (and UNIX systems, if you must) should really be employing proper languages like Python and [insert favourite "agile" language here]?
Re: The Horror
No. These days anything goes and Bash is appropriately qualified. Respect is due to people who enjoy time-tested languages.
Re: Scripting GNU in the 21st Century
Somebody go tell those who are making the 'GNU' autoconf and automake??
Re: Scripting GNU in the 21st Century
.. of course, the enitre point of GNU autotools is to enable you to code programs wuch that they compile regardless of what is and isn't avaliable on the build, host and target platforms, and if we start assuming they're fully GNU compatible it sort of defeats the point a bit, no?
Caution: Theater-Wide Monitor Required (NT)
.