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