]>
Commit | Line | Data |
---|---|---|
d166f048 JA |
1 | # must do this because posix mode causes process substitution to be disabled |
2 | # and flagged as a syntax error, which causes the shell to exit | |
3 | set +o posix | |
4 | ||
726f6388 JA |
5 | expect() |
6 | { | |
7 | echo expect "$@" | |
8 | } | |
9 | ||
10 | HOME=/usr/homes/chet # to make the check against new-exp.right work | |
11 | expect '<foo bar>' | |
12 | recho "${undef-"foo bar"}" # should be foo bar | |
13 | expect '<foo>' | |
14 | recho "${und="foo"}" # should be foo | |
15 | ||
16 | expect "<$HOME>" | |
17 | recho ${HOME-"}"} | |
18 | expect "<$HOME>" | |
19 | recho "${HOME-'}'}" | |
20 | expect "<$HOME>" | |
21 | recho "${HOME-"}"}" | |
22 | ||
ccc6cda3 JA |
23 | expect $0: 'HOME: }: syntax error: operand expected (error token is "}")' |
24 | recho "${HOME:`echo }`}" # should be a math error -- bad substring substitution | |
726f6388 | 25 | |
ccc6cda3 JA |
26 | expect unset |
27 | _ENV=oops | |
cce855bc | 28 | x=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]} |
ccc6cda3 | 29 | echo ${x:-unset} |
726f6388 JA |
30 | |
31 | expect "<$HOME>" | |
32 | recho ${HOME} | |
33 | expect "<$HOME>" | |
34 | recho ${HOME:-`echo }`} | |
35 | expect "<$HOME>" | |
36 | recho ${HOME:-`echo "}"`} | |
37 | expect "<$HOME>" | |
38 | recho "${HOME:-`echo "}"`}" | |
39 | expect "<$HOME>" | |
40 | recho "$(echo "${HOME}")" | |
41 | expect "<$HOME>" | |
42 | recho "$(echo "$(echo ${HOME})")" | |
43 | expect "<$HOME>" | |
44 | recho "$(echo "$(echo "${HOME}")")" | |
45 | ||
46 | P=*@* | |
ccc6cda3 JA |
47 | expect '<*@*>' |
48 | recho "${P%"*"}" # | |
49 | expect '<*@*>' | |
50 | recho "${P%'*'}" # | |
726f6388 JA |
51 | expect '<@*>' |
52 | recho "${P#\*}" # should be @* | |
53 | ||
54 | expect '<)>' | |
55 | recho "$(echo ")")" # should be ) | |
56 | expect '<")">' | |
57 | recho "$(echo "\")\"")" # should be ")" | |
58 | ||
59 | foo='abcd ' | |
60 | expect '<-abcd> <->' | |
61 | recho -${foo}- # should be -abcd - | |
62 | expect '<-abcd> <->' | |
63 | recho -${foo% *}- # should be -abcd - | |
64 | expect '<-abcd->' | |
65 | recho -${foo%% *}- # should be -abcd- | |
66 | ||
726f6388 JA |
67 | foo=bar |
68 | expect '<bar foo>' | |
69 | echo -n $foo' ' ; echo foo | |
70 | ||
71 | expect '<bar foo>' | |
72 | echo -n $foo" " ; echo foo | |
73 | ||
74 | expect '<bar foo>' | |
75 | echo -n "$foo " ; echo foo | |
76 | ||
ccc6cda3 | 77 | expect '<barfoo>' |
726f6388 JA |
78 | echo -e "$foo\c " ; echo foo |
79 | ||
ccc6cda3 | 80 | expect '<barfoo>' |
726f6388 JA |
81 | echo -e $foo"\c " ; echo foo |
82 | ||
d166f048 JA |
83 | # make sure backslashes are preserved in front of characters that are not |
84 | # valid backslash escapes | |
85 | expect '<\x>' | |
86 | echo -e '\x' | |
87 | ||
ccc6cda3 JA |
88 | # substring tests |
89 | z=abcdefghijklmnop | |
90 | expect '<abcd>' | |
91 | recho ${z:0:4} | |
92 | ||
93 | expect '<efg> <nop>' | |
94 | recho ${z:4:3} ${z:${#z}-3:3} | |
95 | ||
d166f048 JA |
96 | expect '<efg> <nop>' |
97 | recho ${z:4:3} ${z: -3:3} | |
98 | ||
ccc6cda3 JA |
99 | expect '<hijklmnop>' |
100 | recho ${z:7:30} | |
101 | ||
102 | expect '<abcdefghijklmnop>' | |
103 | recho ${z:0:100} | |
104 | ||
105 | expect '<abcdefghijklmnop>' | |
106 | recho ${z:0:${#z}} | |
107 | ||
108 | set 'ab cd' 'ef' 'gh ij' 'kl mn' 'op' | |
109 | expect '<ab cd> <ef>' | |
110 | recho "${@:1:2}" | |
111 | ||
112 | expect '<gh ij> <kl mn>' | |
113 | recho "${@:3:2}" | |
114 | ||
115 | expect '<gh ij> <kl mn> <op>' | |
116 | recho "${@:3:4}" | |
117 | ||
118 | expect '<ab cd> <ef> <gh ij> <kl mn> <op>' | |
119 | recho "${@:1:$#}" | |
120 | ||
28ef6c31 JA |
121 | # code to ad-hoc parse arithmetic expressions in substring expansions was |
122 | # broken until post-2.04 | |
123 | base=/home/chet/foo//bar | |
124 | string1=$base/abcabcabc | |
125 | x=1 j=4 | |
126 | ||
127 | expect '</home/chet/foo//bar/abcabcabc>' | |
128 | recho ${string1:0} | |
129 | ||
130 | expect '<home/chet/foo//bar/abcabcabc>' | |
131 | recho ${string1:1} | |
132 | ||
133 | expect '<home>' | |
134 | recho ${string1:(j?1:0):j} | |
135 | ||
136 | expect '<home>' | |
137 | recho ${string1:j?1:0:j} | |
138 | ||
139 | expect '<home>' | |
140 | recho ${string1:(j?(x?1:0):0):j} | |
141 | ||
142 | expect '<home>' | |
143 | recho ${string1:j?(x?1:0):0:j} | |
144 | ||
145 | unset base string1 x j | |
146 | ||
ccc6cda3 JA |
147 | # indirect variable references |
148 | expect '<abcdefghijklmnop>' | |
149 | recho ${!9:-$z} | |
150 | ||
151 | ef=4 | |
152 | expect '<4>' | |
153 | recho ${!2} | |
154 | ||
155 | expect '<op>' | |
156 | recho ${!#} | |
157 | ||
158 | set a b c d e | |
159 | a= | |
160 | expect '<abcdefghijklmnop>' | |
161 | recho ${a:-$z} | |
162 | expect '<abcdefghijklmnop>' | |
163 | recho ${!1:-$z} | |
164 | ||
165 | expect nothing | |
166 | recho ${a-$z} | |
167 | expect nothing | |
168 | recho ${!1-$z} | |
169 | ||
726f6388 JA |
170 | set -u |
171 | expect $0: ABX: unbound variable | |
d166f048 | 172 | ( recho ${ABX} ) |
726f6388 JA |
173 | set +u |
174 | ||
175 | expect $0: '$6: cannot assign in this way' | |
176 | recho ${6="arg6"} | |
177 | ||
ccc6cda3 JA |
178 | v=abcde |
179 | ||
180 | # sed-like variable substitution | |
181 | expect '<xxcde>' | |
182 | recho ${v/a[a-z]/xx} | |
183 | expect '<axxde>' | |
184 | recho ${v/a??/axx} | |
185 | expect '<abxyz>' | |
186 | recho ${v/c??/xyz} | |
187 | expect '<abbcde>' | |
188 | recho ${v/#a/ab} | |
189 | expect '<abcde>' | |
190 | recho ${v/#d/ab} | |
191 | expect '<abcabe>' | |
192 | recho ${v/d/ab} | |
193 | expect '<abcdlast>' | |
194 | recho ${v/%?/last} | |
195 | expect '<abcde>' | |
196 | recho ${v/%x/last} | |
197 | ||
198 | av=(abcd efgh ijkl mnop qrst uvwx) | |
199 | ||
200 | expect '<xxcd>' | |
201 | recho ${av/??/xx} | |
202 | expect '<abxx>' | |
203 | recho ${av/%??/xx} | |
204 | expect '<xxgh>' | |
205 | recho ${av[1]/??/xx} | |
206 | expect '<efgh>' | |
207 | recho ${av[1]/%ab/xx} | |
208 | expect '<xxfgh>' | |
209 | recho ${av[1]/#?/xx} | |
210 | expect '<zagh>' | |
211 | recho ${av[1]/??/za} | |
212 | expect '<zaza>' | |
213 | recho ${av[1]//??/za} | |
214 | expect '<zagh>' | |
215 | recho ${av[1]//#??/za} | |
216 | expect '<efza>' | |
217 | recho ${av[1]//%??/za} | |
218 | ||
219 | expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>' | |
220 | recho ${av[@]/*/yyy} | |
221 | expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>' | |
222 | recho ${av[@]/#*/yyy} | |
223 | expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>' | |
224 | recho ${av[@]/%*/yyy} | |
225 | expect '<yyy> <efgh> <ijkl> <mnop> <qrst> <uvwx>' | |
226 | recho ${av[@]/a*/yyy} | |
227 | expect '<abxx> <efxx> <ijxx> <mnxx> <qrxx> <uvxx>' | |
228 | recho ${av[@]/%??/xx} | |
229 | ||
230 | set abcd efgh ijkl mnop qrst uvwx | |
231 | ||
232 | expect '<xxcd>' | |
233 | recho ${1/??/xx} | |
234 | expect '<xxcd> <xxgh> <xxkl> <xxop> <xxst> <xxwx>' | |
235 | recho ${@/??/xx} | |
236 | expect '<xxcd> <xxgh> <xxkl> <xxop> <xxst> <xxwx>' | |
237 | recho ${@/%??/xx} | |
238 | expect '<zaza>' | |
239 | recho ${3//??/za} | |
240 | expect '<efza>' | |
241 | recho ${3//%??/za} | |
242 | expect '<zaza> <zaza> <zaza> <zaza> <zaza> <zaza>' | |
243 | recho ${@//??/za} | |
244 | expect '<zacd> <zagh> <zakl> <zaop> <zast> <zawx>' | |
245 | recho ${@//#??/za} | |
246 | expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>' | |
247 | recho ${@//*/yyy} | |
248 | expect '<yyy> <efgh> <ijkl> <mnop> <qrst> <uvwx>' | |
249 | recho ${@//a*/yyy} | |
250 | expect '<abcd> <efgh> <ijkl> <mnop> <qrst> <uvwyyy>' | |
251 | recho ${@//%x*/yyy} | |
252 | ||
726f6388 JA |
253 | expect a newline |
254 | echo $abmcde | |
255 | ||
bb70624e JA |
256 | # sneaky way to replace a newline in a variable value with something else |
257 | AVAR=$'This\nstring\nhas\nmultiple\nlines.' | |
258 | echo "${AVAR}" | |
259 | ||
260 | eval BVAR=\"\${AVAR//$'\n'/-}\" | |
261 | echo "$BVAR" | |
262 | ||
263 | unset AVAR BVAR | |
264 | ||
d166f048 JA |
265 | # run process substitution tests in a subshell so that syntax errors |
266 | # caused by a shell not implementing process substitution (e.g., one | |
267 | # built on a NeXT) will not cause the whole test to exit prematurely | |
cce855bc | 268 | ${THIS_SH} ./new-exp1.sub |
ccc6cda3 | 269 | |
cce855bc JA |
270 | # run the tests of $(<filename) in a subshell to avoid cluttering up |
271 | # this script | |
272 | ${THIS_SH} ./new-exp2.sub | |
273 | ||
274 | expect '<6>' | |
275 | recho ${#:-foo} | |
ccc6cda3 JA |
276 | expect $0: '${#:}: bad substitution' |
277 | echo ${#:} | |
278 | ||
279 | expect "<'>" | |
280 | recho "'" | |
281 | expect '<">' | |
282 | recho '"' | |
283 | expect '<"hello">' | |
284 | recho "\"hello\"" | |
285 | ||
286 | shift $# | |
287 | unset foo | |
288 | z=abcdef | |
289 | z1='abc def' | |
290 | ||
291 | expect '<>' | |
292 | recho ${foo:-""} | |
293 | expect nothing | |
294 | recho ${foo:-"$@"} | |
295 | expect '<>' | |
296 | recho "${foo:-$@}" | |
297 | ||
298 | # unset var | |
299 | expect '<>' | |
300 | recho ${foo:-"$zbcd"} | |
301 | expect nothing | |
302 | recho ${foo:-$zbcd} | |
303 | ||
304 | # set var | |
305 | expect '<abcdef>' | |
306 | recho ${foo:-"$z"} | |
307 | expect '<abc def>' | |
308 | recho ${foo:-"$z1"} | |
309 | ||
310 | expect '<abcdef>' | |
311 | recho ${foo:-$z} | |
312 | expect '<abc> <def>' | |
313 | recho ${foo:-$z1} | |
314 | ||
315 | expect '<abcdef>' | |
316 | recho "${foo:-$z}" | |
317 | expect '<abc def>' | |
318 | recho "${foo:-$z1}" | |
319 | ||
320 | expect '<abcdef>' | |
321 | recho "${foo:-"$z"}" | |
322 | # this disagrees with sh and ksh, but I think it is right according | |
323 | # to posix.2. | |
324 | expect '<abc def>' | |
325 | recho "${foo:-"$z1"}" | |
326 | ||
327 | set ab cd ef gh | |
328 | expect '<ab> <cd> <ef> <gh>' | |
329 | recho ${foo:-"$@"} | |
330 | expect '<ab> <cd> <ef> <gh>' | |
331 | recho "${foo:-$@}" | |
332 | expect '<ab> <cd> <ef> <gh>' | |
333 | recho "${foo:-"$@"}" | |
334 | ||
335 | shift $# | |
336 | expect nothing | |
337 | recho $xxx"$@" | |
338 | expect nothing | |
339 | recho ${foo:-$xxx"$@"} | |
340 | expect '<>' | |
341 | recho "${foo:-$xxx$@}" | |
342 | expect '<>' | |
343 | recho "${foo:-$xxx"$@"}" | |
344 | ||
345 | expect nothing | |
346 | recho $xxx"$@" | |
347 | expect nothing | |
348 | recho "$xxx$@" | |
349 | expect nothing | |
350 | recho "$@"$xxx | |
351 | ||
352 | expect '<>' | |
353 | recho $xxx"" | |
354 | expect '<>' | |
355 | recho $xxx'' | |
356 | expect '<>' | |
357 | recho ''$xxx | |
358 | expect '<>' | |
359 | recho ""$xxx | |
360 | ||
361 | AB='abcdefghijklmnopqrstuvwxyz' | |
362 | ||
363 | recho ${AB:7:15} | |
364 | recho ${AB:15:7} | |
365 | ||
366 | recho ${AB:20} | |
367 | ||
368 | recho ${AB:0} | |
369 | recho ${AB:0:20} | |
370 | ||
371 | recho ${AB:10:7} | |
372 | recho ${AB:10:3+4} | |
373 | recho ${AB:20/2:3+4} | |
374 | ||
375 | set 1 2 3 4 5 6 | |
376 | recho \""${*:2:2}"\" | |
377 | ||
378 | IFS=: | |
379 | recho \""${*:2:2}"\" | |
380 | ||
381 | IFS=$' \t\n' | |
382 | ||
383 | z=123456 | |
384 | ||
385 | recho \""${z:2:2}"\" | |
386 | recho \""${z:2}"\" | |
387 | recho \""${z:2:4}"\" | |
388 | recho \""${z:2:6}"\" | |
389 | ||
390 | set $'\1' $'\2' $'\177' | |
391 | ||
392 | recho $* | |
393 | recho $@ | |
394 | ||
395 | recho ${*} | |
396 | recho ${@} | |
397 | ||
398 | xx=one/two/two | |
399 | recho ${xx%/*} | |
400 | recho ${xx/\/two} | |
401 | ||
402 | yy=oneonetwo | |
403 | recho ${yy//one} | |
404 | recho ${yy/\/one} | |
405 | ||
406 | xx=oneonetwo | |
407 | ||
408 | recho ${xx/one} | |
409 | recho ${xx//one} | |
410 | recho ${xx/\/one} | |
411 | ||
d166f048 JA |
412 | # out-of-range substrings |
413 | var=abc | |
414 | c=${var:3} | |
415 | expect nothing | |
416 | recho $c | |
417 | c=${var:4} | |
418 | expect nothing | |
419 | recho $c | |
420 | expect '<./new-exp.tests: -2: substring expression < 0>' | |
421 | c=${var:0:-2} | |
422 | ||
423 | var=abcdefghi | |
424 | c=${var:3:12} | |
425 | recho $c | |
426 | c=${var:4:20} | |
427 | recho $c | |
428 | ||
429 | # make sure null patterns work | |
430 | xxx=endocrine | |
431 | yyy=n | |
432 | unset zzz | |
433 | ||
434 | recho ${xxx/$yyy/*} | |
435 | recho ${xxx//$yyy/*} | |
436 | ||
437 | recho ${xxx/$zzz/*} | |
438 | recho ${xxx//$zzz/*} | |
439 | ||
bb70624e JA |
440 | recho ${xxx//%${zzz}/} |
441 | recho ${xxx//%${zzz}} | |
442 | recho ${xxx//#${zzz}/} | |
443 | recho ${xxx//#${zzz}} | |
444 | ||
d166f048 JA |
445 | # another case that caused a core dump in bash-2.0 |
446 | XPATH=/usr/bin:/bin:/usr/local/bin:/usr/gnu/bin::/usr/bin/X11:/sbin:/usr/sbin | |
447 | ||
448 | recho ${XPATH//:/ } | |
449 | ||
450 | xx=(ar as at au av aw ax ay az) | |
451 | ||
452 | recho ${xx[@]/a/} | |
453 | recho ${xx[@]//a/} | |
454 | ||
455 | recho ${xx[*]/a/} | |
456 | recho ${xx[*]//a/} | |
457 | ||
458 | recho ${xx[@]%?} | |
459 | recho ${xx[*]%?} | |
460 | ||
461 | recho ${xx[@]#?} | |
462 | recho ${xx[*]#?} | |
463 | ||
464 | set -- ar as at au av aw ax ay az | |
465 | ||
466 | recho ${@/a/} | |
467 | recho ${@//a/} | |
468 | ||
469 | recho ${*/a/} | |
470 | recho ${*//a/} | |
471 | ||
472 | recho ${@%?} | |
473 | recho ${*%?} | |
474 | ||
475 | recho ${@#?} | |
476 | recho ${*#?} | |
477 | ||
478 | shift $# | |
479 | set -u | |
480 | ( recho $9 ; echo after 1) | |
481 | ( recho ${9} ; echo after 2) | |
482 | ( recho $UNSET ; echo after 3) | |
483 | ( recho ${UNSET} ; echo after 4) | |
484 | ( recho "$UNSET" ; echo after 5) | |
485 | ( recho "${UNSET}" ; echo after 6) | |
486 | ( recho "${#UNSET}" ; echo after 7) | |
487 | set +u | |
488 | ||
cce855bc JA |
489 | RECEIVED="12345" |
490 | recho "${RECEIVED:$((${#RECEIVED}-1)):1}" | |
491 | RECEIVED="12345#" | |
492 | recho "${RECEIVED:$((${#RECEIVED}-1)):1}" | |
493 | RECEIVED="#" | |
494 | recho "${RECEIVED:$((${#RECEIVED}-1)):1}" | |
495 | RECEIVED="" | |
496 | recho "${RECEIVED:$((${#RECEIVED}-1)):1}" | |
497 | ||
bb70624e JA |
498 | # tests of new prefix expansion ${!prefix*} |
499 | ${THIS_SH} ./new-exp3.sub | |
500 | ||
501 | # these caused errors and core dumps in versions before bash-2.04 | |
502 | c="" | |
503 | echo ${c//${$(($#-1))}/x/} | |
504 | ||
505 | set a b c d e f g | |
506 | recho "$@" | |
507 | ||
508 | set -- ${@:1:$(($# - 2))} | |
509 | recho "$@" | |
510 | ||
511 | set a b | |
512 | recho ${@:1:$(($# - 2))} | |
513 | ||
514 | recho ${@:1:0} | |
515 | recho ${@:1:1} | |
516 | recho ${@:1:2} | |
517 | ||
518 | recho "${*:1:0}" | |
519 | ||
520 | # this is an error -- negative expression | |
521 | set a | |
522 | recho ${@:1:$(($# - 2))} | |
523 | ||
726f6388 JA |
524 | # this must be last! |
525 | expect $0: 'ABXD: parameter unset' | |
526 | recho ${ABXD:?"parameter unset"} |