Work the Shell - When Is “Good Enough” Good Enough?

Shell scripting and obsessive-compulsive perfectionism is almost a contradction in terms.
______________________

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.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

While shell scripts are

A. T. Young's picture

While shell scripts are indeed great for fast prototyping, they're equally useful for mission-critical production code. Look at the scripts that installed your system, and the ones that bring everything up each time you boot.

In addition, even large, complex applications can be written as shell scripts -- especially with the fast hardware we have these days. A few years ago, I got tired of continually re-writing a big nonlinear damped-least-squares program that I'd been using for decades, every time a new problem came up. I noticed that the changes needed were so mechanical that I could let the machine do that work for me. So I now have a 2500-line shell script that looks at the data, infers its format, asks the user a few questions (like what the model is to be fitted), and collects the necessary pieces to form the source code for the program, compiles it, and then executes the program. Ten years ago I'd never have dreamed of putting a compilation inside a shell script, but in fact it all proceeds smoothly, and you hardly notice the little momentary pause while the compilation takes place.

While lots of shell scripts are indeed throwaways that need only to be barely good enough for one-time use, a surprising number turn out to be things you find yourself using again and again -- often in spite of your original intentions. So I've found it pays, in the long run, to take the time to add enough comments to document the code for the time a year from now when I find myself looking at it cold again.

I agree that optimizations are things you only do when you find the thing runs too slowly, though. Often, this is best done by finding the slow part of the script and writing just that one bit as a compiled program in your favorite language, leaving the rest of the procedure as a shell script.

Good enough

martyskitch's picture

Great article and as I tell my kids when they are doing homework "Prefection is the enemy of good enough".

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix