Writing Shell Scripts

Non-trivial shell scripts quickly become unweildy--at least mine do. Incremental debugging then sometimes requires either a) a lot of unnecessary parts of the script run over and over or b) moving if/fi pairs around in the text to avoid sections that are known to be working. Instead, here is the recipe I have developed:

  1. Write the shell script as a series of steps.
  2. Create a function for each step.
  3. Make sure each function independently sets up its own environment (e.g. don't assume that the last function put you in the right directory)
  4. Make sure that each step leaves behind whatever is needed for the next step
  5. Don't clean up until the very end. Make a function that does this.
Now you will have a list of steps at the end of the script. You can turn steps off and on just by prefixing the line with a # or removing it. Keep the clean-up function turned off while you are debugging.

Comments

Popular Posts