Dive into essential Bash script debugging and error handling techniques with these beginner-friendly questions. Enhance your command-line proficiency by identifying common mistakes, understanding debug commands, and learning best practices for reliable scripting.
What happens if you forget the closing 'fi' in a Bash if-statement such as 'if [ $x -eq 1 ]; then echo Match;'?
Explanation: Bash requires every opened if block to be closed with a corresponding 'fi'; forgetting 'fi' leads to a clear syntax error and script termination. 'The script will run successfully' is incorrect as the syntax is incomplete. 'The script will ignore the missing 'fi'' is wrong because Bash does not automatically correct structural mistakes. 'Skipping the if condition' is not possible since Bash enforces proper block structure.
Which Bash option ensures a pipeline returns a failure status if any command fails in a pipeline like 'cmd1 | cmd2'?
Explanation: 'set -o pipefail' causes a pipeline to fail if any command fails, which is important for robust error handling. 'set -e' exits the script on any non-zero exit code but does not address pipelines specifically. 'set +x' disables debugging output and does not affect error status. 'set -v' prints shell input lines but does not help with pipeline failures.
What effect does running a script with 'bash -x script.sh' have while debugging your Bash script?
Explanation: Running with 'bash -x' provides a trace of each command before execution, making it easier to see the flow and locate errors. It does not stop the script for unset variables, which would require 'set -u'. Commenting or prompting for confirmation before every command are not features of the '-x' flag.
If you use 'set -u' at the start of your Bash script, what behavior should you expect when referencing an unset variable like $UNSET_VAR?
Explanation: 'set -u' configures Bash to treat the use of unset variables as an error, terminating the script. Treating it as an empty string is the default behavior without 'set -u'. Bash never sets unset variables to zero by default, nor does it skip lines containing unset variables.
What is the recommended way to check if the last executed command in a Bash script failed?
Explanation: The special variable '$?' always holds the exit status of the last command, which is a standard way to check for failures. '$O' and 'exitstatus' do not exist as Bash variables. '$#' gives the number of positional parameters, not the exit status.
Which construct allows a Bash script to execute a cleanup function automatically when it terminates, such as removing a temporary file?
Explanation: 'trap cleanup EXIT' sets up a trap so that the 'cleanup' function is run when the script exits. 'try' and 'finally' are not Bash concepts. 'onexit' is not a valid Bash statement either, making 'trap' the only correct answer here.
What is a likely effect if you forget to quote variables in a Bash script, for example writing 'rm $file' when $file contains spaces?
Explanation: Omitting quotes around variables containing spaces can cause Bash to split values, leading to possible failure or deletion of the wrong files. The shell does not treat such variables as single arguments without quotes. Ignoring the command or automatically quoting are incorrect, as Bash executes the statement as written.
If you want to save only error messages from a command 'mycmd' into a file called error.log, which command should you use?
Explanation: The '2u003E' syntax redirects standard error to a file, capturing error messages specifically. Using 'u003E' or '1u003E' only redirects standard output, not errors. 'u003Eu003E' appends output, but without '2u003Eu003E', it only affects standard output—not standard error.
Which Bash expression is used to check if a file named 'file.txt' exists before attempting to read it?
Explanation: '[ -f file.txt ]' tests if 'file.txt' exists and is a regular file. '[ -z file.txt ]' checks if the string is empty, not file existence. '{ file.txt }' is not a valid Bash condition. '[ eq file.txt ]' does not conform to Bash test syntax.
Which built-in Bash tool allows step-by-step interactive debugging of scripts at the command line?
Explanation: 'bashdb' is a debugger that provides interactive, step-by-step inspection of Bash scripts. 'grep' searches files for patterns, 'tail' displays the end of files, and 'chmod' changes file permissions—none of which provide interactive script debugging features.