Sometimes scripts need to do something really destructive.
It is generally a good idea to display a warning prompt and provide a way to back out before performing these destructive actions.
It is also useful to provide a mechanism whereby the interactive prompt can be disabled.
This post covers one solution for this problem.
The following script pretends to perform a destructive action when the NUKE_ENVIRONMENT environment variable is set to true.
By default, a prompt is displayed that allows the user to interactively proceed, skip the destructive action but execute the rest
of the script, or abort the script.
If the FORCE environment variable is also set to true, no prompt is displayed.
The script assumes the users knows what they are doing and executes the destructive action unconditionally.
warn.sh complete listing
The script can be tested with the following commands.
This example uses environment variables directly.
A production example my use command line parameters instead.