Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Global variables everyone should use

This is a post about theory. When writing a small program, or any sized program really, you should consider using some global environment variables. These can really help you out, and could prevent you from shutting your computer down (if you happen to be writing a tool that shuts your computer down)! Set DRYRUN to any non-null value to have your script/program only display what it will do or otherwise take no action. This is incredibly important for early-phase testing! You want to make sure your branching works and you get to each intended action, without actually rebooting perhaps. Set VERBOSE to any non-null value to display what it is doing. This could be as verbose as "set -x" or, for example, echo "about to fraternize with the enemy..." before pinging a remote host. Set DEBUG or DEBUG_LEVEL to an integer from 0 to 10 to decide how much verbosity you want. Output should generally go to standard error. And now you can have a conditional for your verbose messages, so DEBUG=1 gets the most basic messages, but DEBUG=10 gets "set -x" or some other scale of your choice. I normally use DEBUG=8 as a threshold for displaying sensitive information. Passwords will be masked up to level 8, but at level 8 and above, even password variables might leak.

echo "This message goes to standard error" 1>&2

What sorts of global variables do you tend to use in your programming that others should use?

Comments