Effective error handling and debugging are critical for writing robust shell scripts. This set of 30 MCQs covers error codes, debugging techniques, and logging strategies to enhance your scripting skills.
Error Handling and Debugging in Shell Scripting – MCQs
1. Exit Codes and Error Checking (10 Questions)
What does an exit code of 0 indicate in a shell script? a) An error occurred b) The script ran successfully c) The script encountered a warning d) The script is still running
Which command returns the exit status of the last executed command in shell scripting? a) status b) $? c) exit d) return
What exit code does a failed command usually return? a) 0 b) 1 c) 2 d) 255
How can you check if the previous command failed in shell scripting? a) if [ $? -eq 1 ] b) if [ $? -eq 0 ] c) if [ $? -ne 0 ] d) if [ $? -ge 0 ]
What exit code is typically used for general errors in a shell script? a) 0 b) 1 c) 2 d) 255
Which command can you use to stop a script immediately upon encountering an error? a) exit b) break c) return d) trap
What does the command exit 0 do in a shell script? a) Terminates the script with a successful status b) Terminates the script with an error status c) Continues executing the script d) Returns an undefined exit code
What is the default exit code for a successfully executed command? a) 0 b) 1 c) 2 d) 255
How do you handle errors by checking the exit code after each command in a script? a) Use the trap command b) Use the if statement with $? c) Use the exit command d) Use the catch command
Which of the following ensures a script exits immediately on any error? a) set -e b) set -x c) set +e d) exit -e
2. Debugging Scripts with set -x and set -e (10 Questions)
What does the set -x command do in shell scripting? a) Enables debugging by printing each command before executing it b) Stops the execution of the script c) Executes commands in a specific order d) Enables error handling
How can you disable debugging in a shell script after using set -x? a) set +x b) set -x c) set stop d) set debug off
What does the set -e command do in a shell script? a) Causes the script to exit immediately if any command fails b) Logs all output to a file c) Allows a script to ignore errors d) Enables verbose output
What happens when set -e is used in a script? a) The script will continue execution even after an error b) The script will stop execution if any command fails c) Debugging will be turned off d) Error messages will not be shown
What is the effect of using both set -x and set -e in a shell script? a) Debugging output will be shown, and the script will exit on errors b) Debugging output will be suppressed, and the script will continue on errors c) Both debugging and error handling will be disabled d) Only debugging output will be shown
Which of the following is the correct syntax to enable debugging in a script? a) set +e b) set -x c) set -debug d) set debug=true
When using set -e, what happens if a command fails and its exit code is not checked? a) The script will stop executing immediately b) The script will continue execution c) The command will be retried d) An error message will be displayed
Which command can be used to track and debug scripts interactively in shell scripting? a) set -x b) debug c) trace d) run
In which scenario would you use set -x in a shell script? a) To display each command and its arguments before execution b) To terminate the script if an error occurs c) To redirect output to a file d) To store the results of commands in a variable
What is the purpose of set +e in shell scripting? a) To disable immediate script termination on error b) To enable debugging output c) To log errors to a file d) To start a new shell session
3. Error Logging (10 Questions)
What is the primary purpose of error logging in shell scripting? a) To provide information about successful commands b) To capture error messages for troubleshooting c) To store outputs of commands d) To execute commands in sequence
How can you log an error message to a file in shell scripting? a) echo "Error message" > logfile b) echo "Error message" 2>> logfile c) echo "Error message" >> logfile d) echo "Error message" > error.log
Which file descriptor is used for standard error (stderr) in shell scripting? a) 0 b) 1 c) 2 d) 3
Which command can you use to log both standard output and standard error to the same file? a) command > logfile 2>&1 b) command >> logfile c) command 2>> logfile d) command > logfile
How can you log errors to both a file and the console? a) command | tee logfile b) command 2>&1 c) command > logfile 2>&1 d) command | log file
Which of the following is a correct way to redirect error output to a log file? a) command > logfile 2>&1 b) command >> logfile c) command 2> logfile d) command > logfile
How can you log the output of a shell script without overwriting the existing log file? a) command >> logfile b) command > logfile c) command 2> logfile d) command &> logfile
What is the purpose of the 2>> operator in shell scripting? a) Redirect standard input to a file b) Append standard error to a file c) Append standard output to a file d) Redirect standard output to a file
How would you capture all output, including errors, in a file in shell scripting? a) command 2>&1 > logfile b) command &> logfile c) command >> logfile d) command > logfile
Which of the following will log error messages generated by a script without modifying the content of the output? a) command 2>&1 | tee error.log b) command 2>> error.log c) command > output.log d) command | tee error.log
Answers
Qno
Answer
1
b) The script ran successfully
2
b) $?
3
b) 1
4
c) if [ $? -ne 0 ]
5
b) 1
6
a) exit
7
a) Terminates the script with a successful status
8
a) 0
9
b) Use the if statement with $?
10
a) set -e
11
a) Enables debugging by printing each command before executing it
12
a) set +x
13
a) Causes the script to exit immediately if any command fails
14
b) The script will stop execution if any command fails
15
a) Debugging output will be shown, and the script will exit on errors
16
b) set -x
17
b) The script will continue execution
18
a) set -x
19
a) To display each command and its arguments before execution
20
a) To disable immediate script termination on error