MCQs on Error Handling and Debugging | Shell Scripting

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)

  1. 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
  2. Which command returns the exit status of the last executed command in shell scripting?
    a) status
    b) $?
    c) exit
    d) return
  3. What exit code does a failed command usually return?
    a) 0
    b) 1
    c) 2
    d) 255
  4. 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 ]
  5. What exit code is typically used for general errors in a shell script?
    a) 0
    b) 1
    c) 2
    d) 255
  6. Which command can you use to stop a script immediately upon encountering an error?
    a) exit
    b) break
    c) return
    d) trap
  7. 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
  8. What is the default exit code for a successfully executed command?
    a) 0
    b) 1
    c) 2
    d) 255
  9. 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
  10. 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)

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. Which command can be used to track and debug scripts interactively in shell scripting?
    a) set -x
    b) debug
    c) trace
    d) run
  9. 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
  10. 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)

  1. 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
  2. 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
  3. Which file descriptor is used for standard error (stderr) in shell scripting?
    a) 0
    b) 1
    c) 2
    d) 3
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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

QnoAnswer
1b) The script ran successfully
2b) $?
3b) 1
4c) if [ $? -ne 0 ]
5b) 1
6a) exit
7a) Terminates the script with a successful status
8a) 0
9b) Use the if statement with $?
10a) set -e
11a) Enables debugging by printing each command before executing it
12a) set +x
13a) Causes the script to exit immediately if any command fails
14b) The script will stop execution if any command fails
15a) Debugging output will be shown, and the script will exit on errors
16b) set -x
17b) The script will continue execution
18a) set -x
19a) To display each command and its arguments before execution
20a) To disable immediate script termination on error
21b) To capture error messages for troubleshooting
22b) echo “Error message” 2>> logfile
23c) 2
24a) command > logfile 2>&1
25a) command
26c) command 2> logfile
27a) command >> logfile
28b) Append standard error to a file
29b) command &> logfile
30b) command 2>> error.log

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top