Bird
0
0

Consider this script snippet:

medium📝 Debug Q14 of 15
Bash Scripting - Error Handling
Consider this script snippet:
mkdir test_dir
false
if [ $? -eq 0 ]; then
  echo "Created"
else
  echo "Failed"
fi

What is the problem if the script always prints "Failed" even when the directory is created?
Amkdir command does not set exit code
BThe syntax of if statement is wrong
CThe <code>$?</code> is checked too late after mkdir
Decho command overwrites the exit code
Step-by-Step Solution
Solution:
  1. Step 1: Understand exit code timing

    The exit code $? must be checked immediately after the command it relates to.
  2. Step 2: Identify delay in checking exit code

    If any command runs between mkdir and checking $?, the exit code changes.
  3. Final Answer:

    The $? is checked too late after mkdir -> Option C
  4. Quick Check:

    Check $? immediately after command [OK]
Quick Trick: Check $? right after command, no commands in between [OK]
Common Mistakes:
MISTAKES
  • Checking $? after other commands
  • Assuming mkdir never fails
  • Misunderstanding if syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes