Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to make the script exit immediately if any command fails.
Bash Scripting
#!/bin/bash [1] echo "Start script" false echo "This will not print if exit on error is set"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set +e' which disables exit on error.
Using 'exit 1' without condition, which exits immediately.
Using 'trap' incorrectly without understanding its purpose.
✗ Incorrect
Using 'set -e' makes the script stop running if any command returns an error.
2fill in blank
mediumComplete the code to enable exit on error and print a message before exiting.
Bash Scripting
#!/bin/bash set -e trap 'echo "Error occurred, exiting."' [1] echo "Running script" false echo "This will not print"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'EXIT' which triggers on any exit, not just errors.
Using 'INT' or 'TERM' which are signals for interrupts or termination.
✗ Incorrect
The 'ERR' trap runs when a command fails, allowing a message before exit.
3fill in blank
hardFix the error in the script to ensure it exits on any command failure.
Bash Scripting
#!/bin/bash [1] echo "Start" false echo "End"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set +e' which disables exit on error.
Using 'set -u' which treats unset variables as errors but doesn't exit on command failure.
✗ Incorrect
'set -e' ensures the script exits immediately on any command failure.
4fill in blank
hardFill both blanks to create a script that exits on error and prints a custom message on failure.
Bash Scripting
#!/bin/bash [1] trap '[2] "Script failed."' ERR echo "Running" false echo "Done"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set +e' disables exit on error.
Using 'exit' in trap without message.
Using wrong commands in trap.
✗ Incorrect
'set -e' enables exit on error, and 'echo' prints the message when trap triggers.
5fill in blank
hardFill all three blanks to create a script that exits on error, prints a message on error, and disables exit on error after a command.
Bash Scripting
#!/bin/bash [1] trap '[2] "Error detected."' ERR echo "Start" set [3] false echo "This will print because exit on error is disabled"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set +e' too early disables exit on error.
Using wrong trap commands.
Confusing '+e' and '-e' options.
✗ Incorrect
'set -e' enables exit on error, 'echo' prints the message on error, and 'set +e' disables exit on error.