1 ## compare_shells: dash bash mksh
2 ## oils_failures_allowed: 0
3
4 # builtin-trap.test.sh
5
6 #### trap accepts/ignores --
7 trap -- 'echo hi' EXIT
8 echo done
9 ## STDOUT:
10 done
11 hi
12 ## END
13
14 #### trap 'echo hi' KILL (regression test, caught by smoosh suite)
15 trap 'echo hi' 9
16 echo status=$?
17 trap 'echo hi' KILL
18 echo status=$?
19 trap 'echo hi' STOP
20 echo status=$?
21 trap 'echo hi' TERM
22 echo status=$?
23 ## STDOUT:
24 status=0
25 status=0
26 status=0
27 status=0
28 ## END
29 ## OK osh STDOUT:
30 status=1
31 status=1
32 status=1
33 status=0
34 ## END
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 EXIT with explicit exit
107 trap 'echo IN TRAP; echo $stdout' EXIT
108 stdout=FOO
109 exit 42
110
111 ## status: 42
112 ## STDOUT:
113 IN TRAP
114 FOO
115 ## END
116
117 #### trap EXIT with command sub / subshell / pipeline
118 trap 'echo EXIT TRAP' EXIT
119
120 echo $(echo command sub)
121
122 ( echo subshell )
123
124 echo pipeline | cat
125
126 ## STDOUT:
127 command sub
128 subshell
129 pipeline
130 EXIT TRAP
131 ## END
132
133 #### trap 0 is equivalent to EXIT
134 # not sure why this is, but POSIX wants it.
135 trap 'echo EXIT' 0
136 echo status=$?
137 trap - EXIT
138 echo status=$?
139 ## status: 0
140 ## STDOUT:
141 status=0
142 status=0
143 ## END
144
145 #### trap 1 is equivalent to SIGHUP; HUP is equivalent to SIGHUP
146 trap 'echo HUP' SIGHUP
147 echo status=$?
148 trap 'echo HUP' HUP
149 echo status=$?
150 trap 'echo HUP' 1
151 echo status=$?
152 trap - HUP
153 echo status=$?
154 ## status: 0
155 ## STDOUT:
156 status=0
157 status=0
158 status=0
159 status=0
160 ## END
161 ## N-I dash STDOUT:
162 status=1
163 status=0
164 status=0
165 status=0
166 ## END
167
168 #### eval in the exit trap (regression for issue #293)
169 trap 'eval "echo hi"' 0
170 ## STDOUT:
171 hi
172 ## END
173
174
175 #### exit codes for traps are isolated
176
177 trap 'echo USR1 trap status=$?; ( exit 42 )' USR1
178
179 echo before=$?
180
181 # Equivalent to 'kill -USR1 $$' except OSH doesn't have "kill" yet.
182 # /bin/kill doesn't exist on Debian unless 'procps' is installed.
183 sh -c "kill -USR1 $$"
184 echo after=$?
185
186 ## STDOUT:
187 before=0
188 USR1 trap status=0
189 after=0
190 ## END
191
192 #### traps are cleared in subshell (started with &)
193
194 # Test with SIGURG because the default handler is SIG_IGN
195 #
196 # If we use SIGUSR1, I think the shell reverts to killing the process
197
198 # https://man7.org/linux/man-pages/man7/signal.7.html
199
200 trap 'echo SIGURG' URG
201
202 kill -URG $$
203
204 # Hm trap doesn't happen here
205 { echo begin child; sleep 0.1; echo end child; } &
206 kill -URG $!
207 wait
208 echo "wait status $?"
209
210 # In the CI, mksh sometimes gives:
211 #
212 # USR1
213 # begin child
214 # done
215 #
216 # leaving off 'end child'. This seems like a BUG to me?
217
218 ## STDOUT:
219 SIGURG
220 begin child
221 end child
222 wait status 0
223 ## END