Comments As Data Shell Pattern

Motivation:

You're writing a shell script. You want the reader of the script to know, right off the bat, how to use the program (the usage message). You want the script to have a usage message when the user asks for something goofy. You do not want the usage message and the usage comment to get out of sync.

Solution:

Make the first comment in the shell script the actual text of the usage message. Use "different" comments (e.g., "##") to distinguish it from the other comments and the leading #!/bin/sh. Then, for your usage function in the script, use the command

 sed -n '/^##/s/^## //p' "$0"

Typical uses:
  1. I use it as described.
  2. Emacs's documentation for commands and functions works roughly the same way.

-- BillTrost

I normally feel that HotComments is an antipattern, but maybe not in this context... --CameronSmith


CategoryUnixShellPattern


EditText of this page (last edited July 30, 2012) or FindPage with title or text search