Challenge - 5 Problems
Error Logging Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Understanding error redirection in Bash
What will be the output and error behavior when running this script?
#!/bin/bash ls /nonexistentfile > output.log 2> error.log
Bash Scripting
ls /nonexistentfile > output.log 2> error.logAttempts:
2 left
💡 Hint
Remember that '2>' redirects standard error and '>' redirects standard output.
✗ Incorrect
The command redirects standard output to output.log and standard error to error.log. Since the file does not exist, ls produces an error message sent to standard error, so error.log contains the message, output.log is empty, and nothing appears on screen.
💻 Command Output
intermediate2:00remaining
Effect of combined output and error redirection
What will be the content of combined.log after running this command?
Assume file.txt does not exist.
grep 'pattern' file.txt &> combined.log
Assume file.txt does not exist.
Bash Scripting
grep 'pattern' file.txt &> combined.logAttempts:
2 left
💡 Hint
The '&>' operator redirects both stdout and stderr to the same file.
✗ Incorrect
Since file.txt does not exist, grep outputs an error message to standard error. The '&>' operator redirects both standard output and standard error to combined.log, so the error message is saved there.
❓ Troubleshoot
advanced2:00remaining
Diagnosing silent failures in a script
A script runs a command that fails, but no error message appears on the terminal or in the log file. Which of these is the most likely cause?
Attempts:
2 left
💡 Hint
Think about where error messages go if redirected to a special device.
✗ Incorrect
Redirecting error output to /dev/null discards error messages, causing silent failures with no visible error messages on screen or in logs.
✅ Best Practice
advanced2:00remaining
Best practice for logging errors in a Bash script
Which option shows the best way to log errors to a file while keeping normal output visible on the terminal?
Attempts:
2 left
💡 Hint
You want errors in a file but normal output on screen.
✗ Incorrect
Redirecting only standard error to a file (2> error.log) keeps normal output on the terminal and logs errors separately, which is a good practice for error logging.
🔀 Workflow
expert3:00remaining
Order of commands to capture and handle errors in a deployment script
Arrange these steps in the correct order to ensure errors are logged and the script exits on failure:
1. Redirect errors to a log file
2. Run deployment command
3. Check command exit status
4. Exit script if error detected
1. Redirect errors to a log file
2. Run deployment command
3. Check command exit status
4. Exit script if error detected
Attempts:
2 left
💡 Hint
Think about setting up logging before running commands and then checking results.
✗ Incorrect
First, set up error redirection so errors are captured. Then run the command. Next, check if it succeeded by examining the exit status. Finally, exit the script if an error occurred.