Let's start our activity as follows:
- Open a terminal and begin a new script called mytrap.sh with the following contents:
#!/bin/bashfunction setup() { trap "cleanup" SIGINT SIGTERM echo "PID of script is $$"}function cleanup() { echo "cleaning up" exit 1}setup# Loop forever with a noop (:)while :do sleep 1done
- Execute the script with $ bash mtrap.sh.
- Press Enter several times and watch the behavior of the program.
- Press Ctrl + C; notice anything different?