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