]>
Commit | Line | Data |
---|---|---|
d166f048 JA |
1 | # test the trap code |
2 | ||
3 | trap 'echo exiting' 0 | |
4 | trap 'echo aborting' 1 2 3 6 15 | |
5 | ||
6 | # make sure a user-specified subshell runs the exit trap, but does not | |
7 | # inherit the exit trap from a parent shell | |
8 | ( trap 'echo subshell exit' 0; exit 0 ) | |
9 | ( exit 0 ) | |
10 | ||
11 | trap | |
12 | ||
13 | func() | |
14 | { | |
7117c2d2 | 15 | trap 'echo ${FUNCNAME:-$0}[$LINENO] funcdebug' DEBUG |
d166f048 JA |
16 | echo funcdebug line |
17 | } | |
18 | ||
19 | trap 'echo [$LINENO] debug' DEBUG | |
20 | echo debug line | |
21 | ||
22 | trap | |
23 | ||
24 | func | |
25 | ||
26 | trap | |
27 | ||
7117c2d2 JA |
28 | trap 'echo ${FUNCNAME:-$0}[$LINENO] debug' DEBUG |
29 | func2() | |
30 | { | |
31 | echo func2debug line | |
32 | } | |
33 | declare -ft func2 | |
34 | func2 | |
35 | ||
36 | unset -f func2 | |
37 | ||
d166f048 JA |
38 | trap '' DEBUG |
39 | ||
40 | trap | |
41 | ||
42 | trap - debug | |
43 | ||
44 | trap | |
45 | ||
46 | trap - HUP | |
47 | trap hup | |
48 | trap '' INT | |
49 | trap '' int | |
50 | ||
51 | trap | |
52 | ||
28ef6c31 JA |
53 | # exit 0 in exit trap should set exit status |
54 | ( | |
55 | set -e | |
56 | trap 'exit 0' EXIT | |
57 | false | |
58 | echo bad | |
59 | ) | |
60 | echo $? | |
61 | ||
d166f048 JA |
62 | # hmmm...should this set the handling to SIG_IGN for children, too? |
63 | trap '' USR2 | |
cce855bc | 64 | ./trap1.sub |
d166f048 | 65 | |
f73dda09 JA |
66 | # test ERR trap |
67 | ./trap2.sub | |
68 | ||
0001803f CR |
69 | ${THIS_SH} ./trap3.sub |
70 | ||
ac50fbac CR |
71 | ${THIS_SH} ./trap4.sub |
72 | ||
73 | # This doesn't work right on all Unix versions | |
74 | #${THIS_SH} ./trap5.sub | |
75 | ||
d166f048 JA |
76 | # |
77 | # show that setting a trap on SIGCHLD is not disastrous. | |
78 | # | |
79 | set -o monitor | |
80 | ||
81 | trap 'echo caught a child death' SIGCHLD | |
82 | ||
83 | sleep 7 & sleep 6 & sleep 5 & | |
84 | ||
3185942a | 85 | # this will only catch the first, since there's a trap on SIGCHLD |
d166f048 JA |
86 | wait |
87 | ||
88 | trap -p SIGCHLD | |
cce855bc JA |
89 | |
90 | # Now reset some of the signals the shell handles specially back to | |
91 | # their default values (with or without the SIG prefix) | |
b80f6443 | 92 | trap - SIGINT QUIT TERM |
cce855bc JA |
93 | |
94 | trap | |
3185942a JA |
95 | |
96 | trap - SIGCHLD | |
97 | wait |