0
0
Bash Scriptingscripting~20 mins

Error logging patterns in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Error Logging Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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.log
AError message appears on screen; output.log and error.log are both empty
BError message appears on screen; output.log contains error message; error.log is empty
CNo output on screen; output.log is empty; error.log contains the error message
DNo output on screen; output.log contains error message; error.log is empty
Attempts:
2 left
💡 Hint
Remember that '2>' redirects standard error and '>' redirects standard output.
💻 Command Output
intermediate
2:00remaining
Effect of combined output and error redirection
What will be the content of combined.log after running this command?
grep 'pattern' file.txt &> combined.log

Assume file.txt does not exist.
Bash Scripting
grep 'pattern' file.txt &> combined.log
Acombined.log contains the error message about missing file
BError message appears on screen; combined.log is empty
Ccombined.log is empty
Dcombined.log contains the output of grep if file.txt existed
Attempts:
2 left
💡 Hint
The '&>' operator redirects both stdout and stderr to the same file.
Troubleshoot
advanced
2: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?
AThe command's error output is redirected to /dev/null
BThe command's output is redirected to a file
CThe script uses 'set -e' to stop on errors
DThe command is run with sudo
Attempts:
2 left
💡 Hint
Think about where error messages go if redirected to a special device.
Best Practice
advanced
2: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?
Acommand > output.log 2>&1
Bcommand 2> error.log
Ccommand &> all.log
Dcommand > output.log 2> error.log
Attempts:
2 left
💡 Hint
You want errors in a file but normal output on screen.
🔀 Workflow
expert
3: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
A1,3,2,4
B2,3,1,4
C2,1,3,4
D1,2,3,4
Attempts:
2 left
💡 Hint
Think about setting up logging before running commands and then checking results.