Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to run the cleanup function when the script exits.
Bash Scripting
trap [1] EXIT cleanup() { echo "Cleaning up..." } cleanup
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'exit' instead of the function name in trap.
Adding parentheses after the function name in trap.
Using 'signal' which is not a command here.
✗ Incorrect
The trap command runs the specified function or command when the script receives the EXIT signal. Here, 'cleanup' is the function to run.
2fill in blank
mediumComplete the code to trap both EXIT and INT signals to run cleanup.
Bash Scripting
trap [1] EXIT INT cleanup() { echo "Cleaning up resources..." } cleanup
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Including parentheses after the function name in trap.
Using signal names as the command to run.
Forgetting to list all signals after the command.
✗ Incorrect
The trap command takes the function name without parentheses to run on specified signals like EXIT and INT (Ctrl+C).
3fill in blank
hardFix the error in the trap command to correctly call cleanup on script exit.
Bash Scripting
trap [1] EXIT cleanup() { echo "Done cleaning." } cleanup
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses after the function name in trap.
Using quotes around the function name.
Using the wrong command name.
✗ Incorrect
In trap, the function name should be given without parentheses or quotes to run correctly on exit.
4fill in blank
hardFill both blanks to create a trap that runs cleanup on EXIT and TERM signals.
Bash Scripting
trap [1] [2] cleanup() { echo "Cleanup done." } cleanup
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting signals before the command.
Using wrong signal names.
Adding parentheses to the function name.
✗ Incorrect
The trap command syntax is: trap command signals. Here, 'cleanup' is the command, and 'EXIT TERM' are the signals.
5fill in blank
hardFill all three blanks to trap cleanup on EXIT and INT signals and define the cleanup function.
Bash Scripting
trap [1] [2] [3]() { echo "Exiting and cleaning up." } cleanup
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'exit' instead of 'cleanup' as the function name.
Not listing both signals.
Adding parentheses to the function name.
✗ Incorrect
The trap command runs 'cleanup' on signals 'EXIT INT'. The function named 'cleanup' is defined to run the cleanup code.