11.2. General Shell Scripting Advice

Before I delve into the project I first want to provide some general tips you should keep in mind when writing shell scripts:

  • Provide usage instructions and help options.

  • Be sure to provide guidance on how to use your scripts. A good summary of what the script does and its argument list is mandatory if you plan on sharing them with others, but should still be included even if you're the only person who will ever use it. I've written several utility scripts and have come back months later and have forgotten how to use them.

  • Comment your code.

  • Writing code to perform common activities solidifies them into procedures and reduces some possibility of human error. Still, just because something is "taken care of" doesn't mean it can be forgotten. Comments can document the steps taken in a human readable format and compliance regulations may require you to comment your code depending on your environment.

  • Know under which account the script will run.

  • In general, shell scripts run with the same privileges of the user account that initiated it. This means the script may not be able to change a file's owner or permissions or to perform other activities typically reserved for a super user. You may need to escalate your privileges using su or sudo to run the script.

  • Direct error messages to STDERR.

  • Command line utilities have a rich tradition and power users have come to expect certain behavior. One such behavior is respecting the use of the standard Unix file ...

Get PHP and MySQL®: Create-Modify-Reuse now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.