Bird
0
0

How can you combine error handling with logging errors to a file in bash?

hard🚀 Application Q9 of 15
Bash Scripting - Error Handling
How can you combine error handling with logging errors to a file in bash?
Acommand || { echo "Error" >> error.log; exit 1; }
Bcommand && echo "Error" >> error.log
Ccommand | echo "Error" >> error.log
Dcommand > error.log
Step-by-Step Solution
Solution:
  1. Step 1: Understand error handling with logging

    Using '||' runs the block only if command fails, logging error and exiting.
  2. Step 2: Analyze other options

    '&&' runs on success, '|' pipes output, '>' redirects output, none handle errors properly.
  3. Final Answer:

    command || { echo "Error" >> error.log; exit 1; } -> Option A
  4. Quick Check:

    Use '||' with logging block for error handling [OK]
Quick Trick: Use '|| { ... }' to log errors and exit [OK]
Common Mistakes:
MISTAKES
  • Using '&&' instead of '||'
  • Misusing pipe for error handling
  • Redirecting output without error check

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes