]> git.ipfire.org Git - thirdparty/bash.git/blob - tests/glob.tests
Bash-5.2-rc4 release
[thirdparty/bash.git] / tests / glob.tests
1 # This program is free software: you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation, either version 3 of the License, or
4 # (at your option) any later version.
5 #
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License
12 # along with this program. If not, see <http://www.gnu.org/licenses/>.
13 #
14 export LC_COLLATE=C
15 #
16 # test the shell globbing
17 #
18 expect()
19 {
20 : # if needed, change me to echo expect "$@"
21 }
22
23 # First, a test that bash-2.01.1 fails
24 ${THIS_SH} ./glob1.sub
25 ${THIS_SH} ./glob2.sub
26 ${THIS_SH} ./glob3.sub
27 ${THIS_SH} ./glob4.sub
28 ${THIS_SH} ./glob5.sub
29 ${THIS_SH} ./glob6.sub
30 ${THIS_SH} ./glob7.sub
31 ${THIS_SH} ./glob8.sub
32 ${THIS_SH} ./glob9.sub
33 ${THIS_SH} ./glob10.sub
34
35 MYDIR=$PWD # save where we are
36
37 TESTDIR=$TMPDIR/glob-test-$$
38 mkdir $TESTDIR
39 builtin cd $TESTDIR || { echo $0: cannot cd to $TESTDIR >&2 ; exit 1; }
40 rm -rf *
41
42 touch a b c d abc abd abe bb bcd ca cb dd de Beware
43 mkdir bdir
44
45 # see if `regular' globbing works right
46 expect '<a> <abc> <abd> <abe> <X*>'
47 recho a* X*
48
49 expect '<a> <abc> <abd> <abe>'
50 recho \a*
51
52 # see if null glob expansion works
53 shopt -s nullglob
54
55 expect '<a> <abc> <abd> <abe>'
56 recho a* X*
57
58 shopt -u nullglob
59
60 # see if the failglob option works
61
62 mkdir tmp
63 touch tmp/l1 tmp/l2 tmp/l3
64 builtin echo tmp/l[12] tmp/*4 tmp/*3
65 shopt -s failglob
66 builtin echo tmp/l[12] tmp/*4 tmp/*3
67 rm -r tmp
68 shopt -u failglob
69
70 # see if the code that expands directories only works
71 expect '<bdir/>'
72 recho b*/
73
74 # Test quoted and unquoted globbing characters
75 expect '<*>'
76 recho \*
77
78 expect '<a*>'
79 recho 'a*'
80
81 expect '<a*>'
82 recho a\*
83
84 expect '<c> <ca> <cb> <a*> <*q*>'
85 recho c* a\* *q*
86
87 expect '<**>'
88 recho "*"*
89
90 expect '<**>'
91 recho \**
92
93 expect '<\.\./*/>'
94 recho "\.\./*/"
95
96 expect '<s/\..*//>'
97 recho 's/\..*//'
98
99 # Pattern from Larry Wall's Configure that caused bash to blow up
100 expect '</^root:/{s/^[^:]*:[^:]*:\([^:]*\).*$/\1/>'
101 recho "/^root:/{s/^[^:]*:[^:]*:\([^:]*\).*"'$'"/\1/"
102
103 # Make sure character classes work properly
104
105 expect '<abc> <abd> <abe> <bb> <cb>'
106 recho [a-c]b*
107
108 expect '<abd> <abe> <bb> <bcd> <bdir> <ca> <cb> <dd> <de>'
109 recho [a-y]*[^c]
110
111 expect '<abd> <abe>'
112 recho a*[^c]
113
114 touch a-b aXb
115 expect '<a-b> <aXb>'
116 recho a[X-]b
117
118 touch .x .y
119 expect '<Beware> <d> <dd> <de>'
120 recho [^a-c]*
121
122 # Make sure that filenames with embedded globbing characters are handled
123 # properly
124 mkdir a\*b
125 > a\*b/ooo
126
127 expect '<a*b/ooo>'
128 recho a\*b/*
129
130 expect '<a*b/ooo>'
131 recho a\*?/*
132
133 expect '<no match>'
134 cmd='echo !7'
135 case "$cmd" in
136 *\\!*) echo match ;;
137 *) echo no match ;;
138 esac
139
140 expect '<not there>'
141 file='r.*'
142 case $file in
143 *.\*) echo not there ;;
144 *) echo there ;;
145 esac
146
147 # examples from the Posix.2 spec (d11.2, p. 243)
148 expect '<abc>'
149 recho a[b]c
150
151 expect '<abc>'
152 recho a["b"]c
153
154 expect '<abc>'
155 recho a[\b]c
156
157 expect '<abc>'
158 recho a?c
159
160 expect '<match 1>'
161 case abc in
162 a"b"c) echo 'match 1' ;;
163 *) echo 'BAD match 1' ;;
164 esac
165
166 expect '<match 2>'
167 case abc in
168 a*c) echo 'match 2' ;;
169 *) echo 'BAD match 2' ;;
170 esac
171
172 expect '<ok 1>'
173 case abc in
174 "a?c") echo 'bad 1' ;;
175 *) echo 'ok 1' ;;
176 esac
177
178 expect '<ok 2>'
179 case abc in
180 a\*c) echo 'bad 2' ;;
181 *) echo 'ok 2' ;;
182 esac
183
184 expect '<ok 3>'
185 case abc in
186 a\[b]c) echo 'bad 3' ;;
187 *) echo 'ok 3' ;;
188 esac
189
190 expect '<ok 4>'
191 case "$nosuchvar" in
192 "") echo 'ok 4' ;;
193 *) echo 'bad 4' ;;
194 esac
195
196 # This is very odd, but sh and ksh seem to agree
197 expect '<ok 5>'
198 case abc in
199 a["\b"]c) echo 'ok 5' ;;
200 *) echo 'bad 5' ;;
201 esac
202
203 mkdir man
204 mkdir man/man1
205 touch man/man1/bash.1
206 expect '<man/man1/bash.1>'
207 recho */man*/bash.*
208 expect '<man/man1/bash.1>'
209 recho $(echo */man*/bash.*)
210 expect '<man/man1/bash.1>'
211 recho "$(echo */man*/bash.*)"
212
213 # tests with multiple `*'s
214 case abc in
215 a***c) echo ok 1;;
216 esac
217
218 case abc in
219 a*****?c) echo ok 2;;
220 esac
221
222 case abc in
223 ?*****??) echo ok 3;;
224 esac
225
226 case abc in
227 *****??) echo ok 4;;
228 esac
229
230 case abc in
231 *****??c) echo ok 5;;
232 esac
233
234 case abc in
235 ?*****?c) echo ok 6;;
236 esac
237
238 case abc in
239 ?***?****c) echo ok 7;;
240 esac
241
242 case abc in
243 ?***?****?) echo ok 8;;
244 esac
245
246 case abc in
247 ?***?****) echo ok 9;;
248 esac
249
250 case abc in
251 *******c) echo ok 10;;
252 esac
253
254 case abc in
255 *******?) echo ok 11;;
256 esac
257
258 case abcdecdhjk in
259 a*cd**?**??k) echo ok 20;;
260 esac
261
262 case abcdecdhjk in
263 a**?**cd**?**??k) echo ok 21;;
264 esac
265
266 case abcdecdhjk in
267 a**?**cd**?**??k***) echo ok 22;;
268 esac
269
270 case abcdecdhjk in
271 a**?**cd**?**??***k) echo ok 23;;
272 esac
273
274 case abcdecdhjk in
275 a**?**cd**?**??***k**) echo ok 24;;
276 esac
277
278 case abcdecdhjk in
279 a****c**?**??*****) echo ok 25;;
280 esac
281
282 case '-' in
283 [-abc]) echo ok 26 ;;
284 esac
285
286 case '-' in
287 [abc-]) echo ok 27 ;;
288 esac
289
290 case '\' in
291 \\) echo ok 28 ;;
292 esac
293
294 case '\' in
295 [\\]) echo ok 29 ;;
296 esac
297
298 case '\' in
299 '\') echo ok 30 ;;
300 esac
301
302 case '[' in
303 [[]) echo ok 31 ;;
304 esac
305
306 # a `[' without a closing `]' is just another character to match, in the
307 # bash implementation
308 case '[' in
309 [) echo ok 32 ;;
310 esac
311
312 case '[abc' in
313 [*) echo 'ok 33';;
314 esac
315
316 # a right bracket shall lose its special meaning and represent itself in
317 # a bracket expression if it occurs first in the list. -- POSIX.2 2.8.3.2
318 case ']' in
319 []]) echo ok 34 ;;
320 esac
321
322 case '-' in
323 []-]) echo ok 35 ;;
324 esac
325
326 # a backslash should just escape the next character in this context
327 case p in
328 [a-\z]) echo ok 36 ;;
329 esac
330
331 # this was a bug in all versions up to bash-2.04-release
332 case "/tmp" in
333 [/\\]*) echo ok 37 ;;
334 esac
335
336 # none of these should output anything
337
338 case abc in
339 ??**********?****?) echo bad 1;;
340 esac
341
342 case abc in
343 ??**********?****c) echo bad 2;;
344 esac
345
346 case abc in
347 ?************c****?****) echo bad 3;;
348 esac
349
350 case abc in
351 *c*?**) echo bad 4;;
352 esac
353
354 case abc in
355 a*****c*?**) echo bad 5;;
356 esac
357
358 case abc in
359 a********???*******) echo bad 6;;
360 esac
361
362 case 'a' in
363 []) echo bad 7 ;;
364 esac
365
366 case '[' in
367 [abc) echo bad 8;;
368 esac
369
370 # let's start testing the case-insensitive globbing code
371 recho b*
372
373 shopt -s nocaseglob
374 recho b*
375
376 recho [b]*
377 shopt -u nocaseglob
378
379 # make sure set -f works right
380 set -f
381 recho *
382 set +f
383
384 # test out the GLOBIGNORE code
385 GLOBIGNORE='.*:*c:*e:?'
386 recho *
387
388 GLOBIGNORE='.*:*b:*d:?'
389 recho *
390
391 # see if GLOBIGNORE can substitute for `set -f'
392 GLOBIGNORE='.*:*'
393 recho *
394
395 unset GLOBIGNORE
396 expect '<man/man1/bash.1>'
397 recho */man*/bash.*
398
399 # make sure null values for GLOBIGNORE have no effect
400 GLOBIGNORE=
401 expect '<man/man1/bash.1>'
402 recho */man*/bash.*
403
404 # this is for the benefit of pure coverage, so it writes the pcv file
405 # in the right place, and for gprof
406 builtin cd $MYDIR
407
408 rm -rf $TESTDIR
409
410 exit 0