Every sysadmin has to deal with shell scripts sometimes.  Besides being a great way to automate tasks, shell scripts are an integral part of the boot/shutdown process.  Unfortunately, too many sysadmins tend to write their shell scripts quick and dirty.  The goal of the "Advanced shell programming" training taught by Mike Ciavarella is to teach admins to write advanced scripts.  In the context of this training, "advanced" doesn't necessarily mean "new tricks you've never heard of before", but instead means writing code that is

robust, portable, secure, and maintainable.

Writing sane shell scripts isn't always as easy as it may seem.  Different vendors have slightly different implementations of the same basic shell.  There's also little guarantee that the tools needed are in the same location, or even exist on the system at all.  Environment variables (especially PATH and LOCALE) can change from system-to-system, or even user-to-user, causing the potential for significant changes in behavior.  Thus, it is best to assume as little as possible about the system the script will run on.

The first step in writing a good shell script is to determine if it is appropriate.  In many cases, a shell script is the best choice, especially if being used in the boot process, across multiple platforms, or when you're short on time.  If the task at hand requires a lot of pattern matching or complicated regular expressions, a tool like Perl is more suitable.

Once it's been determined that the shell is the way to go, it's time to start thinking about the code.  Before a single line of code has been written, it's important to think through what the script will do and write a description in the comments.  The technically optional header can take several forms, but the '/usr/bin/env' form is preferred as helps prevent user-defined environment variables from causing unforeseen outcomes.

One common theme was leaning on the shell to do work when possible.  Checking for a value in a shell script and then supplying a default value if necessary is often done in three or four lines, but the smart admin will use the ${variablename:-defaultvalue} syntax and accomplish the same task on a single line.  As Mike said, "try to be a good, lazy sysadmin. Don't spend time writing code you don't have to write."

This rule doesn't always apply when security is a consideration, though.  Because some environment variables can determine which programs get invoked, it's important to set variables like PATH and IFS to known quantities at the beginning of the script and after each invocation of the . command.  In general, it's best to use specific commands when possible and to use single quotes unless variable or glob expansion is needed.

The idea that was newest to me was the concept of signal trapping.  I knew it existed, but I had never tried implementing it.  Most of my shell scripts are sufficiently short and unimportant that I didn't think trapping necessary.  Seeing as I'm prone to using temporary files in scripts (although many of those can be replaced with sufficiently complicated pipes), trapping makes good sense in order to ensure the script cleans up after itself.

There were more important points in the four hour class than I can fit into a reasonably-sized blog post.  If you're interested in the topic further, including enhancing the performance of shell scripts, you'll have to attend the next offering of this course.  Just make sure to get there early, as Tuesday morning's session needed extra chairs.