1 ## our_shell: ysh
2
3 #### trap --remove INT EXIT
4
5 trap --add INT EXIT HUP {
6 echo one
7 echo two
8 }
9 trap -p > traps.txt
10 wc -l traps.txt
11 echo ---
12
13 trap --remove INT EXIT
14 trap -p > traps.txt
15 wc -l traps.txt
16 echo ---
17
18 trap --add EXIT { echo 'exit' }
19
20 ## STDOUT:
21 3 traps.txt
22 ---
23 1 traps.txt
24 ---
25 exit
26 ## END
27
28 #### trap block arg is a not a closure - like cd and other builtins
29
30 # e.g. We're not using ctx_EnclosedFrame in RunTrapsOnExit() and in the signal
31 # handlers. It's more similar to OSH.
32
33 var x = 'global'
34
35 proc register {
36 var x = 'local'
37 trap --add EXIT {
38 echo "x = $x"
39 }
40 }
41
42 register
43
44 ## STDOUT:
45 x = global
46 ## END
47
48 #### trap --ignore INT USR1
49
50 trap --ignore INT USR1
51 trap -p
52
53 ## STDOUT:
54 trap -- '' SIGINT
55 trap -- '' SIGUSR1
56 ## END
57
58 #### trap --ignore removes hooks (like trap -)
59
60 trap --ignore EXIT
61 echo done
62
63 ## STDOUT:
64 done
65 ## END
66
67 #### trap --ignore with uncatchable STOP signal
68
69 # YSH is stricter and returns an error, which triggers errexit
70 trap --ignore STOP
71 echo done
72
73 ## status: 2
74 ## STDOUT:
75 ## END