summaryrefslogtreecommitdiff
path: root/tests/new-exp.tests
blob: e0c2702a0a49aefd67984688e59ca933f8d7a862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# must do this because posix mode causes process substitution to be disabled
# and flagged as a syntax error, which causes the shell to exit
set +o posix

expect()
{
        echo expect "$@"
}

HOME=/usr/homes/chet	# to make the check against new-exp.right work
expect '<foo bar>'
recho "${undef-"foo bar"}"	# should be foo bar
expect '<foo>'
recho "${und="foo"}"		# should be foo

expect "<$HOME>"
recho ${HOME-"}"}
expect "<$HOME>"
recho "${HOME-'}'}"
expect "<$HOME>"
recho "${HOME-"}"}"

expect $0: 'HOME: }: syntax error: operand expected (error token is "}")'
recho "${HOME:`echo }`}"	# should be a math error -- bad substring substitution

expect unset
_ENV=oops
x=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]}
echo ${x:-unset}

expect "<$HOME>"
recho ${HOME}
expect "<$HOME>"
recho ${HOME:-`echo }`}
expect "<$HOME>"
recho ${HOME:-`echo "}"`}
expect "<$HOME>"
recho "${HOME:-`echo "}"`}"
expect "<$HOME>"
recho "$(echo "${HOME}")"
expect "<$HOME>"
recho "$(echo "$(echo ${HOME})")"
expect "<$HOME>"
recho "$(echo "$(echo "${HOME}")")"

P=*@*
expect '<*@>'
recho "${P%"*"}"	# 
expect '<*@>'
recho "${P%'*'}"	# 
expect '<@*>'
recho "${P#\*}"		# should be @*

expect '<)>'
recho "$(echo ")")"	# should be )
expect '<")">'
recho "$(echo "\")\"")"	# should be ")"

foo='abcd   '
expect '<-abcd> <->'
recho -${foo}-		# should be -abcd -
expect '<-abcd> <->'
recho -${foo% *}-	# should be -abcd -
expect '<-abcd->'
recho -${foo%% *}-	# should be -abcd-

foo=bar
expect '<bar foo>'
echo -n $foo' ' ; echo foo

expect '<bar foo>'
echo -n $foo" " ; echo foo

expect '<bar foo>'
echo -n "$foo " ; echo foo

expect '<barfoo>'
echo -e "$foo\c " ; echo foo

expect '<barfoo>'
echo -e $foo"\c " ; echo foo

# make sure backslashes are preserved in front of characters that are not
# valid backslash escapes
expect '<\x>'
echo -e '\x'

# substring tests
z=abcdefghijklmnop
expect '<abcd>'
recho ${z:0:4}

expect '<efg> <nop>'
recho ${z:4:3} ${z:${#z}-3:3}

expect '<efg> <nop>'
recho ${z:4:3} ${z: -3:3}

expect '<hijklmnop>'
recho ${z:7:30}

expect '<abcdefghijklmnop>'
recho ${z:0:100}

expect '<abcdefghijklmnop>'
recho ${z:0:${#z}}

set 'ab cd' 'ef' 'gh ij' 'kl mn' 'op'
expect '<ab cd> <ef>'
recho "${@:1:2}"

expect '<gh ij> <kl mn>'
recho "${@:3:2}"

expect '<gh ij> <kl mn> <op>'
recho "${@:3:4}"

expect '<ab cd> <ef> <gh ij> <kl mn> <op>'
recho "${@:1:$#}"

# code to ad-hoc parse arithmetic expressions in substring expansions was
# broken until post-2.04
base=/home/chet/foo//bar
string1=$base/abcabcabc
x=1 j=4

expect '</home/chet/foo//bar/abcabcabc>'
recho ${string1:0}

expect '<home/chet/foo//bar/abcabcabc>'
recho ${string1:1}

expect '<home>'
recho ${string1:(j?1:0):j}

expect '<home>'
recho ${string1:j?1:0:j}

expect '<home>'
recho ${string1:(j?(x?1:0):0):j}

expect '<home>'
recho ${string1:j?(x?1:0):0:j}

unset base string1 x j

# indirect variable references
expect '<abcdefghijklmnop>'
recho ${!9:-$z}

ef=4
expect '<4>'
recho ${!2}

expect '<op>'
recho ${!#}

set a b c d e
a=
expect '<abcdefghijklmnop>'
recho ${a:-$z}
expect '<abcdefghijklmnop>'
recho ${!1:-$z}

expect nothing
recho ${a-$z}
expect nothing
recho ${!1-$z}

set -u
expect $0: ABX: unbound variable
( recho ${ABX} )
set +u

expect $0: '$6: cannot assign in this way'
recho ${6="arg6"}

v=abcde

# sed-like variable substitution
expect '<xxcde>'
recho ${v/a[a-z]/xx}
expect '<axxde>'
recho ${v/a??/axx}
expect '<abxyz>'
recho ${v/c??/xyz}
expect '<abbcde>'
recho ${v/#a/ab}
expect '<abcde>'
recho ${v/#d/ab}
expect '<abcabe>'
recho ${v/d/ab}
expect '<abcdlast>'
recho ${v/%?/last}
expect '<abcde>'
recho ${v/%x/last}

av=(abcd efgh ijkl mnop qrst uvwx)

expect '<xxcd>'
recho ${av/??/xx}
expect '<abxx>'
recho ${av/%??/xx}
expect '<xxgh>'
recho ${av[1]/??/xx}
expect '<efgh>'
recho ${av[1]/%ab/xx}
expect '<xxfgh>'
recho ${av[1]/#?/xx}
expect '<zagh>'
recho ${av[1]/??/za}
expect '<zaza>'
recho ${av[1]//??/za}
expect '<zagh>'
recho ${av[1]//#??/za}
expect '<efza>'
recho ${av[1]//%??/za}

expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>'
recho ${av[@]/*/yyy}
expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>'
recho ${av[@]/#*/yyy}
expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>'
recho ${av[@]/%*/yyy}
expect '<yyy> <efgh> <ijkl> <mnop> <qrst> <uvwx>'
recho ${av[@]/a*/yyy}
expect '<abxx> <efxx> <ijxx> <mnxx> <qrxx> <uvxx>'
recho ${av[@]/%??/xx}

set abcd efgh ijkl mnop qrst uvwx

expect '<xxcd>'
recho ${1/??/xx}
expect '<xxcd> <xxgh> <xxkl> <xxop> <xxst> <xxwx>'
recho ${@/??/xx}
expect '<xxcd> <xxgh> <xxkl> <xxop> <xxst> <xxwx>'
recho ${@/%??/xx}
expect '<zaza>'
recho ${3//??/za}
expect '<efza>'
recho ${3//%??/za}
expect '<zaza> <zaza> <zaza> <zaza> <zaza> <zaza>'
recho ${@//??/za}
expect '<zacd> <zagh> <zakl> <zaop> <zast> <zawx>'
recho ${@//#??/za}
expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>'
recho ${@//*/yyy}
expect '<yyy> <efgh> <ijkl> <mnop> <qrst> <uvwx>'
recho ${@//a*/yyy}
expect '<abcd> <efgh> <ijkl> <mnop> <qrst> <uvwyyy>'
recho ${@//%x*/yyy}

expect a newline
echo $abmcde

# sneaky way to replace a newline in a variable value with something else
AVAR=$'This\nstring\nhas\nmultiple\nlines.'
echo "${AVAR}"

eval BVAR=\"\${AVAR//$'\n'/-}\"
echo "$BVAR"

unset AVAR BVAR

# run process substitution tests in a subshell so that syntax errors
# caused by a shell not implementing process substitution (e.g., one
# built on a NeXT) will not cause the whole test to exit prematurely
${THIS_SH} ./new-exp1.sub

# run the tests of $(<filename) in a subshell to avoid cluttering up
# this script
${THIS_SH} ./new-exp2.sub

expect '<6>'
recho ${#:-foo}
expect $0: '${#:}: bad substitution'
echo ${#:}

expect "<'>"
recho "'"
expect '<">'
recho '"'
expect '<"hello">'
recho "\"hello\""

shift $#
unset foo
z=abcdef
z1='abc def'

expect '<>'
recho ${foo:-""}
expect nothing
recho ${foo:-"$@"}
expect '<>'
recho "${foo:-$@}"

# unset var
expect '<>'
recho ${foo:-"$zbcd"}
expect nothing
recho ${foo:-$zbcd}

# set var
expect '<abcdef>'
recho ${foo:-"$z"}
expect '<abc def>'
recho ${foo:-"$z1"}

expect '<abcdef>'
recho ${foo:-$z}
expect '<abc> <def>'
recho ${foo:-$z1}

expect '<abcdef>'
recho "${foo:-$z}"
expect '<abc def>'
recho "${foo:-$z1}"

expect '<abcdef>'
recho "${foo:-"$z"}"
# this disagrees with sh and ksh, but I think it is right according
# to posix.2.
expect '<abc def>'
recho "${foo:-"$z1"}"

set ab cd ef gh
expect '<ab> <cd> <ef> <gh>'
recho ${foo:-"$@"}
expect '<ab> <cd> <ef> <gh>'
recho "${foo:-$@}"
expect '<ab> <cd> <ef> <gh>'
recho "${foo:-"$@"}"

shift $#
expect nothing
recho $xxx"$@"
expect nothing
recho ${foo:-$xxx"$@"}
expect '<>'
recho "${foo:-$xxx$@}"
expect '<>'
recho "${foo:-$xxx"$@"}"

expect nothing
recho $xxx"$@"
expect nothing
recho "$xxx$@"
expect nothing
recho "$@"$xxx

expect '<>'
recho $xxx""
expect '<>'
recho $xxx''
expect '<>'
recho ''$xxx
expect '<>'
recho ""$xxx

AB='abcdefghijklmnopqrstuvwxyz'

recho ${AB:7:15}
recho ${AB:15:7}

recho ${AB:20}

recho ${AB:0}
recho ${AB:0:20}

recho ${AB:10:7}
recho ${AB:10:3+4}
recho ${AB:20/2:3+4}

set 1 2 3 4 5 6
recho \""${*:2:2}"\"

IFS=:
recho \""${*:2:2}"\"

IFS=$' \t\n'

z=123456

recho \""${z:2:2}"\"
recho \""${z:2}"\"
recho \""${z:2:4}"\"
recho \""${z:2:6}"\"

set $'\1' $'\2' $'\177'

recho $*
recho $@

recho ${*}
recho ${@}

xx=one/two/two
recho ${xx%/*}
recho ${xx/\/two}

yy=oneonetwo
recho ${yy//one}
recho ${yy/\/one}

xx=oneonetwo

recho ${xx/one}
recho ${xx//one}
recho ${xx/\/one}

# out-of-range substrings
var=abc
c=${var:3}
expect nothing
recho $c
c=${var:4}
expect nothing
recho $c
expect '<./new-exp.tests: -2: substring expression < 0>'
c=${var:0:-2}

var=abcdefghi
c=${var:3:12}
recho $c
c=${var:4:20}
recho $c

# make sure null patterns work
xxx=endocrine
yyy=n
unset zzz

recho ${xxx/$yyy/*}
recho ${xxx//$yyy/*}

recho ${xxx/$zzz/*}
recho ${xxx//$zzz/*}

recho ${xxx//%${zzz}/}
recho ${xxx//%${zzz}}
recho ${xxx//#${zzz}/}
recho ${xxx//#${zzz}}

# another case that caused a core dump in bash-2.0
XPATH=/usr/bin:/bin:/usr/local/bin:/usr/gnu/bin::/usr/bin/X11:/sbin:/usr/sbin

recho ${XPATH//:/ }

xx=(ar as at au av aw ax ay az)

recho ${xx[@]/a/}
recho ${xx[@]//a/}

recho ${xx[*]/a/}
recho ${xx[*]//a/}

recho ${xx[@]%?}
recho ${xx[*]%?}

recho ${xx[@]#?}
recho ${xx[*]#?}

set -- ar as at au av aw ax ay az

recho ${@/a/}
recho ${@//a/}

recho ${*/a/}
recho ${*//a/}

recho ${@%?}
recho ${*%?}

recho ${@#?}
recho ${*#?}

shift $#
set -u
( recho $9 ; echo after 1)
( recho ${9} ; echo after 2)
( recho $UNSET ; echo after 3)
( recho ${UNSET} ; echo after 4)
( recho "$UNSET" ; echo after 5)
( recho "${UNSET}" ; echo after 6)
( recho "${#UNSET}" ; echo after 7)
set +u

RECEIVED="12345"
recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
RECEIVED="12345#"
recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
RECEIVED="#"
recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
RECEIVED=""
recho "${RECEIVED:$((${#RECEIVED}-1)):1}"

# tests of new prefix expansion ${!prefix*}
${THIS_SH} ./new-exp3.sub

# bug with indirect expansion through bash-2.05b
${THIS_SH} ./new-exp4.sub

# these caused errors and core dumps in versions before bash-2.04
c=""
echo ${c//${$(($#-1))}/x/}

set a b c d e f g
recho "$@"

set -- ${@:1:$(($# - 2))}
recho "$@"

set a b
recho ${@:1:$(($# - 2))}

recho ${@:1:0}
recho ${@:1:1}
recho ${@:1:2}

recho "${*:1:0}"

# this is an error -- negative expression
set a
recho ${@:1:$(($# - 2))}

XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
set $( IFS=: ; echo $XPATH ) 

recho ${@##*/}
recho ${@%%[!/]*}

recho ${@#/*}
recho ${@%*/}

set /full/path/to/x16 /another/full/path

recho ${1%/*}
recho ${1%%[!/]*}
recho ${1#*/}
recho ${1##*/}

${THIS_SH} ./new-exp5.sub

unset var
var=blah

# these had better agree
echo ${var[@]:3}
echo ${var:3}
echo ${var[@]//#/--}
echo ${var//#/--}
echo ${var[@]##?}
echo ${var##?}

# this must be last!
expect $0: 'ABXD: parameter unset'
recho ${ABXD:?"parameter unset"}