0
0
Bash Scriptingscripting~20 mins

Why error handling prevents silent failures in Bash Scripting - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Error Handling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
πŸ’» Command Output
intermediate
2:00remaining
What is the output when a command fails without error handling?
Consider this bash script snippet:
#!/bin/bash
mkdir /root/testdir
 echo "Directory created"

What will be the output when run as a normal user without root privileges?
Bash Scripting
#!/bin/bash
mkdir /root/testdir
 echo "Directory created"
ANo output at all
BDirectory created
Cmkdir: cannot create directory β€˜/root/testdir’: Permission denied
D
mkdir: cannot create directory β€˜/root/testdir’: Permission denied
Directory created
Attempts:
2 left
πŸ’‘ Hint
Think about what happens when mkdir fails but the script continues.
🧠 Conceptual
intermediate
1:30remaining
Why use 'set -e' in bash scripts?
What is the main effect of adding set -e at the start of a bash script?
AIt makes the script exit immediately if any command returns a non-zero status.
BIt runs the script in debug mode.
CIt prints all commands before executing them.
DIt ignores all errors and continues running the script.
Attempts:
2 left
πŸ’‘ Hint
Think about how to stop a script when something goes wrong.
πŸ”§ Debug
advanced
2:30remaining
Identify the error handling problem in this script
What is wrong with the error handling in this bash script?
#!/bin/bash
cp /source/file.txt /dest/
if [ $? -eq 0 ]; then
  echo "Copy succeeded"
else
  echo "Copy failed"
fi
rm /source/file.txt
Bash Scripting
#!/bin/bash
cp /source/file.txt /dest/
if [ $? -eq 0 ]; then
  echo "Copy succeeded"
else
  echo "Copy failed"
fi
rm /source/file.txt
AThe script uses $? incorrectly to check the command status.
BThe script does not check if /dest/ exists before copying.
CThe script deletes the source file even if the copy failed, causing data loss.
DThe script should use double brackets [[ ]] instead of single brackets [ ].
Attempts:
2 left
πŸ’‘ Hint
Look at what happens after the copy command regardless of success.
πŸš€ Application
advanced
2:30remaining
How to handle errors in a pipeline to avoid silent failures?
Given this pipeline:
cat file.txt | grep 'pattern' | sort > output.txt

Which approach ensures the script detects if any command in the pipeline fails?
ACheck only the exit status of the last command <code>sort</code>.
BUse <code>set -o pipefail</code> before running the pipeline.
CAdd <code>|| exit 1</code> after the pipeline.
DRedirect errors to /dev/null to hide them.
Attempts:
2 left
πŸ’‘ Hint
By default, bash only returns the last command's status in a pipeline.
🧠 Conceptual
expert
3:00remaining
Why is explicit error handling better than ignoring errors in automation scripts?
In automation scripts, why is it important to handle errors explicitly rather than ignoring them?
AExplicit error handling helps detect problems early and prevents cascading failures.
BIgnoring errors makes scripts run faster and more efficiently.
CErrors are rare and usually do not affect automation outcomes.
DExplicit error handling is only needed for interactive scripts, not automation.
Attempts:
2 left
πŸ’‘ Hint
Think about what happens if a script continues after a failure unnoticed.