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