Debugging Scripts
Debug Flag
Choose Verbose Mode
Many shell scripts use a flag to decide whether to print normal output or extra diagnostic detail.
Program
Play the script to choose debug mode and see the planned diagnostic output.
debug_flag.sh
#!/usr/bin/env bash
debug=
if [[ "$debug" -eq 1 ]]; then
mode="verbose"
else
mode="normal"
fi
echo "debug:$mode"
#!/usr/bin/env bash
debug=
if [[ "$debug" -eq 1 ]]; then
mode="verbose"
else
mode="normal"
fi
echo "debug:$mode"
debug flag
A small numeric flag can switch a script between normal and verbose behavior.
diagnostic mode
Verbose mode should reveal decisions without changing the real result.
branch
The `if` statement copies the selected behavior into one output variable.