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