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