Unix: Writing more maintainable shell scripts


If you often find yourself writing scripts from scratch when you know you wrote something similar not all that long ago, maybe it's time to make your scripts a bit more reusable. The usefulness of an old script -- one that you wrote to serve some particular purpose and then more or less forgot about -- can be greatly enhanced by a few techniques that enhance a script's readability and maintainability. Here are some tricks that I've run across.

One technique that I've found that helps to keep scripts useful is to define its major variables at the top of the script -- making them easier to find and change. If the values are variables are defined at the top of a script with some quick explanations, you often don't have to read deeply into the script to change how it works.



Always indent your if, while, until, and case commands so that they form a visual block of code. This greatly facilitates following the logic from beginning to end.

Use case statements in lieu of long embedded if-thens whenever you can. They are muxh easier to parse and easier still to insert additional logic into.

Use exit codes that help identify why a script terminates prematurely. When you test a script and see it's exited with a return code of 3, you will easily be able to identify the commands that were being run when the script failed.

Use the longer spelled out options on commands rather than the shorter versions if it makes the commands more clear (especially for less commonly used commands). A --verbose might make a lot more sense than -v to someone who is not too clear on some command that you're using.

Try using the set -e command to make your script exits when a command fails. Instead of having to look back through all of a script's output to find the errors, you stop immediately when there's a problem. You can always then add || true for commands that you want to allow to fail without exiting.

Use the set -o nounset (no unset) setting if you want your script to exit when it tries to use an undeclared variable. This can also save you a lot of time when trying to figure out what went wrong, particularly if you haven't looked at the script in several years.

Use functions for chunks of code you might otherwise need to repeat, but make sure the name of your function is clear enough that you can easily understand what it's doing.

Functions are also very useful if you want to use the same code in numerous scripts. You can create a "library" of your better functions and source it in whatever scripts require their use.

If you have a function like this defined in your functions file, you can use it in other scripts by just inserting the line .

Photo Credit: 

flickr / iriskh

Join us:






Operating SystemsWhite Papers & Webcasts

See more White Papers | Webcasts

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Ask a Question