====================== Shell Script Checklist - a list of things to verify in your shell scripts ====================== -IAN! idallen@idallen.ca - script must start with a complete script header (see file script_style.txt) (interpreter, description, syntax, purpose, label, PATH, umask, collate) - validate the number of script command line arguments before using them - issue warnings or errors if arguments are missing or extra arguments are present; do not ignore input; do not process bad or missing data - validate all user input (from command line or via "read" statements): - validate correct type (numbers, strings, files, pathnames, etc.) - validate correct permissions of pathname arguments - issue warnings or errors if input isn't valid; don't process bad data - always issue error messages on stderr (not on stdout) - error messages must appear on stderr so they don't get redirected - error messages must contain the script name, as well as useful help on (a) exactly what was found and (b) what was expected - never say just "illegal input" or "invalid input" or "too many" - always specify how many is "too many" or "too few" - Example: echo 1>&2 "$0: Expecting 3 file names; found $# ($*)" - issue prompts on stderr before reading any input from the user - prompt messages must appear on stderr so they don't get redirected - note: menus are also considered to be prompts - (Optional: you don't need to prompt if stdin is not a terminal) - quote every use of a shell variable, and don't miss any - quote every use, every time, to prevent blank and wildcard expansion - you might get away with not quoting $$, $#, $!, and $?, but that's all - I mean every use, every time; I *really* do mean it; really. - check the return/exit codes of important commands in the script - no need to check "echo" commands that write to screen - pick the important commands (don't clutter the whole script) - exit the script non-zero (bad status) when things go seriously wrong - don't keep processing bad data; print an error and exit the script - exit if arguments are missing, incorrect, superfluous, or not valid - exit if any important commands fail - use good programming style - indent your code correctly inside control structures (basically the same rules as in C language) - break up lines longer than 80 characters (break and continue long lines using a backslash to escape the newline) - add useful, helpful comments (not useless ones) in front of code blocks (comment code blocks; don't comment in front of every single line) - use good variable names, related to your algorithm (don't copy the poor names used by your instructor in examples) - don't duplicate or repeat similar code (parametrize your algorithm) - test your scripts with problematic input - try wildcards, blanks, dashes, punctuation, empty strings, EOF, etc. - try no command line arguments and then hundreds of arguments - try short inputs, medium inputs, and huge long inputs - no input should cause a shell script to generate an internal error