Bird
Raised Fist0
Bash Scriptingscripting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Why is error handling important in bash scripts?
easy
A. It automatically fixes errors without user input
B. It makes scripts run faster
C. It shows when something goes wrong instead of failing silently
D. It reduces the size of the script

Solution

  1. Step 1: Understand silent failures

    Silent failures happen when errors occur but the script continues without warning, causing hidden problems.
  2. Step 2: Role of error handling

    Error handling alerts the user immediately when something goes wrong, preventing unnoticed issues.
  3. Final Answer:

    It shows when something goes wrong instead of failing silently -> Option C
  4. Quick Check:

    Error handling prevents silent failures = D [OK]
Hint: Error handling alerts you to problems early [OK]
Common Mistakes:
  • Thinking error handling speeds up scripts
  • Believing error handling reduces script size
  • Assuming error handling fixes errors automatically
2. Which of the following is the correct way to handle an error after a command in bash?
easy
A. command | echo "Error occurred"
B. command || echo "Error occurred"
C. command && echo "Error occurred"
D. command > echo "Error occurred"

Solution

  1. Step 1: Understand the use of || operator

    The || operator runs the right side only if the left side command fails (returns non-zero).
  2. Step 2: Apply to error handling

    Using command || echo "Error occurred" prints the error message only if the command fails.
  3. Final Answer:

    command || echo "Error occurred" -> Option B
  4. Quick Check:

    Use || for error handling = C [OK]
Hint: Use || to run error message if command fails [OK]
Common Mistakes:
  • Using && instead of || for error handling
  • Using pipe | which connects output, not error handling
  • Redirecting output incorrectly with >
3. What will be the output of this script?
mkdir /root/testdir || echo "Failed to create directory"
echo "Script finished"
medium
A. Failed to create directory Script finished
B. Script finished
C. mkdir: cannot create directory '/root/testdir': Permission denied Script finished
D. No output

Solution

  1. Step 1: Analyze mkdir command

    Creating a directory in /root usually requires root permission, so mkdir will fail with permission denied.
  2. Step 2: Check error handling

    Because mkdir fails, the || operator triggers the echo "Failed to create directory" message.
  3. Final Answer:

    Failed to create directory Script finished -> Option A
  4. Quick Check:

    mkdir fails triggers echo = B [OK]
Hint: If command fails, || runs the error message [OK]
Common Mistakes:
  • Assuming mkdir succeeds without permission
  • Expecting no error message printed
  • Confusing command error output with echo message
4. Identify the error in this script snippet that tries to handle errors:
cp file1.txt /backup/
if [ $? -eq 0 ]
then
  echo "Copy succeeded"
else
  echo "Copy failed"
fi
medium
A. Missing semicolon after if condition
B. Incorrect use of $? for error checking
C. Missing quotes around file names
D. No error, script works correctly

Solution

  1. Step 1: Check if statement syntax

    The if condition [ $? -eq 0 ] is followed by a newline before 'then', which is valid bash syntax. No semicolon is required in this multi-line format.
  2. Step 2: Verify other elements

    $? correctly captures the exit status of cp. Quotes around file names/paths are optional here.
  3. Final Answer:

    No error, script works correctly -> Option D
  4. Quick Check:

    Multi-line if syntax valid without semicolon = C [OK]
Hint: Put ; or newline before then in if statements [OK]
Common Mistakes:
  • Thinking the if statement has a syntax error
  • Believing $? cannot be used for error checking
  • Assuming quotes are mandatory for file names here
5. You want to run a script that copies files and stops immediately if any copy fails, showing an error message. Which approach best prevents silent failures?
hard
A. Use set -e at the start and cp source dest || echo "Copy failed" for each copy
B. Ignore errors and continue copying all files
C. Use only cp source dest without checks
D. Redirect all errors to /dev/null to hide them

Solution

  1. Step 1: Understand set -e behavior

    set -e makes the script exit immediately if any command fails, preventing silent failures.
  2. Step 2: Combine with error messages

    Using cp source dest || echo "Copy failed" shows a clear error message when a copy fails before exiting.
  3. Final Answer:

    Use set -e at the start and cp source dest || echo "Copy failed" for each copy -> Option A
  4. Quick Check:

    set -e + error messages stop silent failures = A [OK]
Hint: Use set -e plus || echo for safe scripts [OK]
Common Mistakes:
  • Ignoring errors and continuing blindly
  • Not using set -e to stop on failure
  • Hiding errors by redirecting to /dev/null
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard