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