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