Bird
0
0

You want to run a command and print "Success" if it succeeds, or "Failed" if it fails, using exit codes. Which script snippet correctly does this?

hard🚀 Application Q8 of 15
Bash Scripting - Error Handling
You want to run a command and print "Success" if it succeeds, or "Failed" if it fails, using exit codes. Which script snippet correctly does this?
Acommand if [ $? ]; then echo "Success"; else echo "Failed"; fi
Bcommand if [ $? = 0 ]; then echo "Success"; else echo "Failed"; fi
Ccommand if [ $? -ne 0 ]; then echo "Success"; else echo "Failed"; fi
Dcommand if [ $? -eq 0 ]; then echo "Success"; else echo "Failed"; fi
Step-by-Step Solution
Solution:
  1. Step 1: Check correct numeric comparison

    Use -eq 0 to test if exit code equals zero (success).
  2. Step 2: Confirm logic for success and failure

    If exit code is 0, print "Success"; else print "Failed".
  3. Final Answer:

    command if [ $? -eq 0 ]; then echo "Success"; else echo "Failed"; fi -> Option D
  4. Quick Check:

    Use -eq 0 for success check [OK]
Quick Trick: Use if [ $? -eq 0 ] for success, else for failure [OK]
Common Mistakes:
MISTAKES
  • Using = instead of -eq
  • Reversing success/failure logic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes