0
0
Bash Scriptingscripting~10 mins

trap for cleanup on exit 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 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'
Asignal
Bexit
Ctrap
Dcleanup
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.
2fill in blank
medium

Complete 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'
AINT
Bcleanup()
Ccleanup
Dexit
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.
3fill in blank
hard

Fix 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'
Acleanup()
Bcleanup
C"cleanup"
D'cleanup()'
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.
4fill in blank
hard

Fill 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'
Acleanup
BEXIT
CTERM
DB C
Attempts:
3 left
💡 Hint
Common Mistakes
Putting signals before the command.
Using wrong signal names.
Adding parentheses to the function name.
5fill in blank
hard

Fill 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'
Acleanup
BEXIT INT
Dexit
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.