OILS / spec / var-op-strip.test.sh View on Github | oilshell.org

357 lines, 190 significant
1
2#### Remove const suffix
3v=abcd
4echo ${v%d} ${v%%cd}
5## stdout: abc ab
6
7#### Remove const prefix
8v=abcd
9echo ${v#a} ${v##ab}
10## stdout: bcd cd
11
12#### Remove const suffix is vectorized on user array
13a=(1a 2a 3a)
14argv.py ${a[@]%a}
15## stdout: ['1', '2', '3']
16## status: 0
17## N-I dash/mksh/ash stdout-json: ""
18## N-I dash/ash status: 2
19## N-I mksh status: 1
20
21#### Remove const suffix is vectorized on $@ array
22set -- 1a 2a 3a
23argv.py ${@%a}
24## stdout: ['1', '2', '3']
25## status: 0
26## N-I dash/ash stdout: ['1a', '2a', '3']
27## N-I dash/ash status: 0
28## N-I mksh stdout-json: ""
29## N-I mksh status: 1
30
31#### Remove const suffix from undefined
32echo ${undef%suffix}
33## stdout:
34
35#### Remove shortest glob suffix
36v=aabbccdd
37echo ${v%c*}
38## stdout: aabbc
39
40#### Remove longest glob suffix
41v=aabbccdd
42echo ${v%%c*}
43## stdout: aabb
44
45#### Remove shortest glob prefix
46v=aabbccdd
47echo ${v#*b}
48## stdout: bccdd
49
50#### Remove longest glob prefix
51v=aabbccdd
52echo ${v##*b}
53## stdout: ccdd
54
55#### Strip char class
56v=abc
57echo ${v%[[:alpha:]]}
58## stdout: ab
59## N-I mksh stdout: abc
60
61#### Strip unicode prefix
62# NOTE: LANG is set to utf-8.
63v='μ-'
64echo ${v#?} # ? is a glob that stands for one character
65echo ${v##?}
66v=''
67echo ${v%?} # ? is a glob that stands for one character
68echo ${v%%?}
69## STDOUT:
70-
71-
72-
73-
74## BUG dash/mksh/ash stdout-repr: '\xbc-\n\xbc-\n-\xce\n-\xce\n'
75## BUG zsh stdout-repr: '\n\n\n\n'
76
77#### Bug fix: Test that you can remove everything with glob
78s='--x--'
79argv.py "${s%%-*}" "${s%-*}" "${s#*-}" "${s##*-}"
80## STDOUT:
81['', '--x-', '-x--', '']
82## END
83
84#### Test that you can remove everything with const
85s='abcd'
86argv.py "${s%%abcd}" "${s%abcd}" "${s#abcd}" "${s##abcd}"
87# failure case:
88argv.py "${s%%abcde}" "${s%abcde}" "${s#abcde}" "${s##abcde}"
89## STDOUT:
90['', '', '', '']
91['abcd', 'abcd', 'abcd', 'abcd']
92## END
93
94#### Prepend using replacement of #
95# This case was found in Kubernetes and others
96array=(aa bb '')
97argv.py ${array[@]/#/prefix-}
98## STDOUT:
99['prefix-aa', 'prefix-bb', 'prefix-']
100## END
101## N-I dash/ash status: 2
102## N-I dash/ash stdout-json: ""
103## N-I mksh status: 1
104## N-I mksh stdout-json: ""
105
106#### Append using replacement of %
107array=(aa bb '')
108argv.py ${array[@]/%/-suffix}
109## STDOUT:
110['aa-suffix', 'bb-suffix', '-suffix']
111## END
112## N-I dash/ash status: 2
113## N-I dash/ash stdout-json: ""
114## N-I mksh status: 1
115## N-I mksh stdout-json: ""
116
117#### strip unquoted and quoted [
118# I guess dash and mksh treat unquoted [ as an invalid glob?
119var='[foo]'
120echo ${var#[}
121echo ${var#"["}
122echo "${var#[}"
123echo "${var#"["}"
124## STDOUT:
125foo]
126foo]
127foo]
128foo]
129## END
130## OK mksh STDOUT:
131[foo]
132foo]
133[foo]
134foo]
135## END
136## BUG zsh stdout-json: ""
137## BUG zsh status: 1
138
139#### strip unquoted and quoted []
140# LooksLikeGlob('[]') is true
141# I guess dash, mksh, and zsh treat unquoted [ as an invalid glob?
142var='[]foo[]'
143echo ${var#[]}
144echo ${var#"[]"}
145echo "${var#[]}"
146echo "${var#"[]"}"
147## STDOUT:
148foo[]
149foo[]
150foo[]
151foo[]
152## END
153## OK mksh/zsh STDOUT:
154[]foo[]
155foo[]
156[]foo[]
157foo[]
158## END
159
160#### strip unquoted and quoted ?
161var='[foo]'
162echo ${var#?}
163echo ${var#"?"}
164echo "${var#?}"
165echo "${var#"?"}"
166## STDOUT:
167foo]
168[foo]
169foo]
170[foo]
171## END
172
173#### strip unquoted and quoted [a]
174var='[a]foo[]'
175echo ${var#[a]}
176echo ${var#"[a]"}
177echo "${var#[a]}"
178echo "${var#"[a]"}"
179## STDOUT:
180[a]foo[]
181foo[]
182[a]foo[]
183foo[]
184## END
185
186#### Nested % and # operators (bug reported by Crestwave)
187var=$'\n'
188argv.py "${var#?}"
189argv.py "${var%''}"
190argv.py "${var%"${var#?}"}"
191var='a'
192argv.py "${var#?}"
193argv.py "${var%''}"
194argv.py "${var%"${var#?}"}"
195## STDOUT:
196['']
197['\n']
198['\n']
199['']
200['a']
201['a']
202## END
203## N-I dash STDOUT:
204['\\n']
205['$\\n']
206['$']
207['']
208['a']
209['a']
210## END
211
212#### strip * (bug regression)
213x=abc
214argv.py "${x#*}"
215argv.py "${x##*}"
216argv.py "${x%*}"
217argv.py "${x%%*}"
218## STDOUT:
219['abc']
220['']
221['abc']
222['']
223## END
224## BUG zsh STDOUT:
225['abc']
226['']
227['ab']
228['']
229## END
230
231#### strip ?
232x=abc
233argv.py "${x#?}"
234argv.py "${x##?}"
235argv.py "${x%?}"
236argv.py "${x%%?}"
237## STDOUT:
238['bc']
239['bc']
240['ab']
241['ab']
242## END
243
244#### strip all
245x=abc
246argv.py "${x#abc}"
247argv.py "${x##abc}"
248argv.py "${x%abc}"
249argv.py "${x%%abc}"
250## STDOUT:
251['']
252['']
253['']
254['']
255## END
256
257#### strip none
258x=abc
259argv.py "${x#}"
260argv.py "${x##}"
261argv.py "${x%}"
262argv.py "${x%%}"
263## STDOUT:
264['abc']
265['abc']
266['abc']
267['abc']
268## END
269
270#### strip all unicode
271x=μabcμ
272echo "${x#?abc?}"
273echo "${x##?abc?}"
274echo "${x%?abc?}"
275echo "${x%%?abc?}"
276## STDOUT:
277
278
279
280
281## BUG dash/mksh/ash STDOUT:
282μabcμ
283μabcμ
284μabcμ
285μabcμ
286## END
287
288#### strip none unicode
289x=μabcμ
290argv.py "${x#}"
291argv.py "${x##}"
292argv.py "${x%}"
293argv.py "${x%%}"
294## STDOUT:
295['\xce\xbcabc\xce\xbc']
296['\xce\xbcabc\xce\xbc']
297['\xce\xbcabc\xce\xbc']
298['\xce\xbcabc\xce\xbc']
299## END
300
301#### Strip Right Brace (#702)
302var='$foo'
303echo 1 "${var#$foo}"
304echo 2 "${var#\$foo}"
305
306var='}'
307echo 10 "${var#}}"
308echo 11 "${var#\}}"
309echo 12 "${var#'}'}"
310echo 13 "${var#"}"}"
311## STDOUT:
3121 $foo
3132
31410 }}
31511
31612
31713
318## END
319## BUG zsh STDOUT:
3201 $foo
3212
32210 }}
32311
32412 }'}
32513
326## END
327
328#### \(\) in pattern (regression)
329x='foo()'
330echo 1 ${x%*\(\)}
331echo 2 ${x%%*\(\)}
332echo 3 ${x#*\(\)}
333echo 4 ${x##*\(\)}
334## STDOUT:
3351 foo
3362
3373
3384
339## END
340
341#### extglob in pattern
342case $SH in (dash|zsh|ash) exit ;; esac
343
344shopt -s extglob
345
346x='foo()'
347echo 1 ${x%*(foo|bar)'()'}
348echo 2 ${x%%*(foo|bar)'()'}
349echo 3 ${x#*(foo|bar)'()'}
350echo 4 ${x##*(foo|bar)'()'}
351## STDOUT:
3521 foo
3532
3543
3554
356## END
357## N-I dash/zsh/ash stdout-json: ""