-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrap_example7.sh
More file actions
44 lines (40 loc) · 1.21 KB
/
trap_example7.sh
File metadata and controls
44 lines (40 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash
# Fred Denis -- August 21st 2020 -- fred.denis3@gmail.com
# Trap command example
#
on_exit() {
printf "\033[1;36m%s\033[m\n" "Delete tempfile"
rm -f "${TEMPFILE}"
printf "\033[1;36m%s\033[m\n" "Check if the tempfile still exists"
ls -ltr "${TEMPFILE}"
printf "\033[1;36m%s\033[m\n" "Sending an email to let people know the script is finished"
}
on_term() {
printf "\033[1;31m%s\033[m\n" "I have been killed !"
exit 123
}
on_int() {
printf "\n\033[1;31m%s\033[m\n" "You hit CTRL-C, are you sure ? (y/n)"
read answer
if [[ ${answer} = "y" ]]; then
printf "\033[1;31m%s\033[m\n" "OK, lets quit then"
exit 456
else
printf "\033[1;31m%s\033[m\n" "OK, lets continue then"
fi
}
show_info() {
printf "\033[1;36m%s\033[m\n" "Show info about the script, select count(*) from a log table, etc ..."
}
trap on_term TERM SEGV
trap on_exit EXIT
trap on_int INT
trap show_info USR1
TEMPFILE=$(mktemp)
printf "\033[1;36m%s\033[m\n" "Tempfile is ${TEMPFILE}"
printf "\033[1;36m%s\033[m\n" "Sending an email to let people know the script is starting"
printf "\033[1;36m%s\033[m\n" "I am PID $$"
printf "\033[1;36m%s\033[m\n" "A first sleep"
sleep 20
printf "\033[1;36m%s\033[m\n" "A second sleep"
sleep 10