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