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