1 #!/bin/bash
2
3 #### trap -l
4 trap -l | grep INT >/dev/null
5 ## status: 0
6 ## N-I dash/mksh status: 1
7
8 #### trap -p
9 trap 'echo exit' EXIT
10 trap -p | grep EXIT >/dev/null
11 ## status: 0
12 ## N-I dash/mksh status: 1
13
14 #### Register invalid trap
15 trap 'foo' SIGINVALID
16 ## status: 1
17
18 #### Remove invalid trap
19 trap - SIGINVALID
20 ## status: 1
21
22 #### SIGINT and INT are aliases
23 trap - SIGINT
24 echo $?
25 trap - INT
26 echo $?
27 ## STDOUT:
28 0
29 0
30 ## END
31 ## N-I dash STDOUT:
32 1
33 0
34 ## END
35
36 #### Invalid trap invocation
37 trap 'foo'
38 echo status=$?
39 ## stdout: status=2
40 ## OK dash stdout: status=1
41 ## BUG mksh stdout: status=0
42
43 #### exit 1 when trap code string is invalid
44 # All shells spew warnings to stderr, but don't actually exit! Bad!
45 trap 'echo <' EXIT
46 echo status=$?
47 ## stdout: status=1
48 ## BUG mksh status: 1
49 ## BUG mksh stdout: status=0
50 ## BUG dash/bash status: 0
51 ## BUG dash/bash stdout: status=0
52
53 #### trap EXIT
54 cleanup() {
55 echo "cleanup [$@]"
56 exit 42
57 }
58 trap 'cleanup x y z' EXIT
59 ## stdout: cleanup [x y z]
60 ## status: 42
61
62 #### trap DEBUG
63 debuglog() {
64 echo "debuglog [$@]"
65 }
66 trap 'debuglog x y' DEBUG
67 echo 1
68 echo 2
69 ## STDOUT:
70 debuglog [x y]
71 1
72 debuglog [x y]
73 2
74 ## END
75 ## N-I dash/mksh STDOUT:
76 1
77 2
78 ## END
79
80 #### trap RETURN
81 profile() {
82 echo "profile [$@]"
83 }
84 g() {
85 echo --
86 echo g
87 echo --
88 return
89 }
90 f() {
91 echo --
92 echo f
93 echo --
94 g
95 }
96 # RETURN trap doesn't fire when a function returns, only when a script returns?
97 # That's not what the manual syas.
98 trap 'profile x y' RETURN
99 f
100 . spec/testdata/return-helper.sh
101 ## status: 42
102 ## STDOUT:
103 --
104 f
105 --
106 --
107 g
108 --
109 return-helper.sh
110 profile [x y]
111 ## END
112 ## N-I dash/mksh STDOUT:
113 --
114 f
115 --
116 --
117 g
118 --
119 return-helper.sh
120 ## END
121
122 #### trap ERR and disable it
123 err() {
124 echo "err [$@] $?"
125 }
126 trap 'err x y' ERR
127 echo 1
128 false
129 echo 2
130 trap - ERR # disable trap
131 false
132 echo 3
133 ## STDOUT:
134 1
135 err [x y] 1
136 2
137 3
138 ## END
139 ## N-I dash STDOUT:
140 1
141 2
142 3
143 ## END
144
145 #### trap with PARSE error (implicit exit)
146 trap 'echo FAILED' EXIT
147 for
148 ## stdout: FAILED
149 ## status: 2
150 ## OK mksh status: 1
151
152 #### trap with PARSE error with explicit exit
153 trap 'echo FAILED; exit 0' EXIT
154 for
155 ## stdout: FAILED
156 ## status: 0
157
158 #### trap 0 is equivalent to EXIT
159 # not sure why this is, but POSIX wants it.
160 trap 'echo EXIT' 0
161 echo status=$?
162 trap - EXIT
163 echo status=$?
164 ## status: 0
165 ## STDOUT:
166 status=0
167 status=0
168 ## END
169
170 #### trap 1 is equivalent to SIGHUP; HUP is equivalent to SIGHUP
171 trap 'echo HUP' SIGHUP
172 echo status=$?
173 trap 'echo HUP' HUP
174 echo status=$?
175 trap 'echo HUP' 1
176 echo status=$?
177 trap - HUP
178 echo status=$?
179 ## status: 0
180 ## STDOUT:
181 status=0
182 status=0
183 status=0
184 status=0
185 ## END
186 ## N-I dash STDOUT:
187 status=1
188 status=0
189 status=0
190 status=0
191 ## END
192
193 #### eval in the exit trap (regression for issue #293)
194 trap 'eval "echo hi"' 0
195 ## STDOUT:
196 hi
197 ## END