0
0
Bash Scriptingscripting~10 mins

set -e for exit on error in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aset -e
Bexit 1
Cset +e
Dtrap 'exit' ERR
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.
2fill in blank
medium

Complete 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'
AEXIT
BERR
CTERM
DINT
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.
3fill in blank
hard

Fix 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'
Aset +e
Bset -x
Cset -e
Dset -u
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.
4fill in blank
hard

Fill 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'
Aset -e
Becho
Cexit
Dset +e
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set +e' disables exit on error.
Using 'exit' in trap without message.
Using wrong commands in trap.
5fill in blank
hard

Fill 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'
Aset -e
Becho
C-x
D+e
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set +e' too early disables exit on error.
Using wrong trap commands.
Confusing '+e' and '-e' options.