1
2 ## compare_shells: bash dash mksh
3 ## oils_failures_allowed: 3
4
5 #### Case statement
6 case a in
7 a) echo A ;;
8 *) echo star ;;
9 esac
10
11 for x in a b; do
12 case $x in
13 # the pattern is DYNAMIC and evaluated on every iteration
14 $x) echo loop ;;
15 *) echo star ;;
16 esac
17 done
18 ## STDOUT:
19 A
20 loop
21 loop
22 ## END
23
24 #### Case statement with ;;&
25 # ;;& keeps testing conditions
26 # NOTE: ;& and ;;& are bash 4 only, no on Mac
27 case a in
28 a) echo A ;;&
29 *) echo star ;;&
30 *) echo star2 ;;
31 esac
32 ## status: 0
33 ## STDOUT:
34 A
35 star
36 star2
37 ## END
38 ## N-I dash stdout-json: ""
39 ## N-I dash status: 2
40
41 #### Case statement with ;&
42 # ;& ignores the next condition. Why would that be useful?
43 case a in
44 a) echo A ;&
45 XX) echo two ;&
46 YY) echo three ;;
47 esac
48 ## status: 0
49 ## STDOUT:
50 A
51 two
52 three
53 ## END
54 ## N-I dash stdout-json: ""
55 ## N-I dash status: 2
56
57 #### Case with empty condition
58 case $empty in
59 ''|foo) echo match ;;
60 *) echo no ;;
61 esac
62 ## stdout: match
63
64 #### Match a literal with a glob character
65 x='*.py'
66 case "$x" in
67 '*.py') echo match ;;
68 esac
69 ## stdout: match
70
71 #### Match a literal with a glob character with a dynamic pattern
72 x='b.py'
73 pat='[ab].py'
74 case "$x" in
75 $pat) echo match ;;
76 esac
77 ## stdout: match
78
79 #### Quoted literal in glob pattern
80 x='[ab].py'
81 pat='[ab].py'
82 case "$x" in
83 "$pat") echo match ;;
84 esac
85 ## stdout: match
86
87 #### Multiple Patterns Match
88 x=foo
89 result='-'
90 case "$x" in
91 f*|*o) result="$result X"
92 esac
93 echo $result
94 ## stdout: - X
95
96 #### Match one unicode char
97
98 # These two code points form a single character.
99 two_code_points="__$(echo $'\u0061\u0300')__"
100
101 # U+0061 is A, and U+0300 is an accent.
102 #
103 # (Example taken from # https://blog.golang.org/strings)
104 #
105 # However ? in bash/zsh only counts CODE POINTS. They do NOT take into account
106 # this case.
107
108 for s in '__a__' '__μ__' "$two_code_points"; do
109 case $s in
110 __?__)
111 echo yes
112 ;;
113 *)
114 echo no
115 esac
116 done
117 ## STDOUT:
118 yes
119 yes
120 no
121 ## END
122 ## BUG dash/mksh STDOUT:
123 yes
124 no
125 no
126 ## END
127
128 #### case with single byte LC_ALL=C
129
130 LC_ALL=C
131
132 c=$(printf \\377)
133
134 # OSH prints -1 here
135 #echo "${#c}"
136
137 case $c in
138 '') echo a ;;
139 "$c") echo b ;;
140 esac
141
142 ## STDOUT:
143 b
144 ## END
145
146 #### \(\) in pattern (regression)
147 s='foo()'
148
149 case $s in
150 *\(\)) echo 'match'
151 esac
152
153 case $SH in (dash) exit;; esac # not implemented
154
155 shopt -s extglob
156
157 case $s in
158 *(foo|bar)'()') echo 'extglob'
159 esac
160 ## STDOUT:
161 match
162 extglob
163 ## END
164 ## N-I dash STDOUT:
165 match
166 ## END
167
168
169 #### case \n bug regression
170
171 case
172 in esac
173
174 ## STDOUT:
175 ## END
176 ## status: 2
177 ## OK mksh status: 1
178