Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/ksh93/tests/builtins.sh
1810 views
1
########################################################################
2
# #
3
# This software is part of the ast package #
4
# Copyright (c) 1982-2012 AT&T Intellectual Property #
5
# and is licensed under the #
6
# Eclipse Public License, Version 1.0 #
7
# by AT&T Intellectual Property #
8
# #
9
# A copy of the License is available at #
10
# http://www.eclipse.org/org/documents/epl-v10.html #
11
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
12
# #
13
# Information and Software Systems Research #
14
# AT&T Research #
15
# Florham Park NJ #
16
# #
17
# David Korn <[email protected]> #
18
# #
19
########################################################################
20
function err_exit
21
{
22
print -u2 -n "\t"
23
print -u2 -r ${Command}[$1]: "${@:2}"
24
let Errors+=1
25
}
26
alias err_exit='err_exit $LINENO'
27
28
Command=${0##*/}
29
integer Errors=0
30
31
tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32
trap "cd /; rm -rf $tmp" EXIT
33
34
# test shell builtin commands
35
builtin getconf
36
: ${foo=bar} || err_exit ": failed"
37
[[ $foo == bar ]] || err_exit ": side effects failed"
38
set -- - foobar
39
[[ $# == 2 && $1 == - && $2 == foobar ]] || err_exit "set -- - foobar failed"
40
set -- -x foobar
41
[[ $# == 2 && $1 == -x && $2 == foobar ]] || err_exit "set -- -x foobar failed"
42
getopts :x: foo || err_exit "getopts :x: returns false"
43
[[ $foo == x && $OPTARG == foobar ]] || err_exit "getopts :x: failed"
44
OPTIND=1
45
getopts :r:s var -r
46
if [[ $var != : || $OPTARG != r ]]
47
then err_exit "'getopts :r:s var -r' not working"
48
fi
49
OPTIND=1
50
getopts :d#u OPT -d 16177
51
if [[ $OPT != d || $OPTARG != 16177 ]]
52
then err_exit "'getopts :d#u OPT=d OPTARG=16177' failed -- OPT=$OPT OPTARG=$OPTARG"
53
fi
54
OPTIND=1
55
while getopts 'ab' option -a -b
56
do [[ $OPTIND == $((OPTIND)) ]] || err_exit "OPTIND optimization bug"
57
done
58
59
USAGE=$'[-][S:server?Operate on the specified \asubservice\a:]:[subservice:=pmserver]
60
{
61
[p:pmserver]
62
[r:repserver]
63
[11:notifyd]
64
}'
65
set pmser p rep r notifyd -11
66
while (( $# > 1 ))
67
do OPTIND=1
68
getopts "$USAGE" OPT -S $1
69
[[ $OPT == S && $OPTARG == $2 ]] || err_exit "OPT=$OPT OPTARG=$OPTARG -- expected OPT=S OPTARG=$2"
70
shift 2
71
done
72
73
false ${foo=bar} && err_exit "false failed"
74
read <<!
75
hello world
76
!
77
[[ $REPLY == 'hello world' ]] || err_exit "read builtin failed"
78
print x:y | IFS=: read a b
79
if [[ $a != x ]]
80
then err_exit "IFS=: read ... not working"
81
fi
82
read <<!
83
hello \
84
world
85
!
86
[[ $REPLY == 'hello world' ]] || err_exit "read continuation failed"
87
read -d x <<!
88
hello worldxfoobar
89
!
90
[[ $REPLY == 'hello world' ]] || err_exit "read builtin failed"
91
read <<\!
92
hello \
93
world \
94
95
!
96
[[ $REPLY == 'hello world' ]] || err_exit "read continuation2 failed"
97
print "one\ntwo" | { read line
98
print $line | /bin/cat > /dev/null
99
read line
100
}
101
read <<\!
102
\
103
a\
104
\
105
\
106
b
107
!
108
if [[ $REPLY != ab ]]
109
then err_exit "read multiple continuation failed"
110
fi
111
if [[ $line != two ]]
112
then err_exit "read from pipeline failed"
113
fi
114
line=two
115
read line < /dev/null
116
if [[ $line != "" ]]
117
then err_exit "read from /dev/null failed"
118
fi
119
if [[ $(print -R -) != - ]]
120
then err_exit "print -R not working correctly"
121
fi
122
if [[ $(print -- -) != - ]]
123
then err_exit "print -- not working correctly"
124
fi
125
print -f "hello%nbar\n" size > /dev/null
126
if (( size != 5 ))
127
then err_exit "%n format of printf not working"
128
fi
129
print -n -u2 2>&1-
130
[[ -w /dev/fd/1 ]] || err_exit "2<&1- with built-ins has side effects"
131
x=$0
132
if [[ $(eval 'print $0') != $x ]]
133
then err_exit '$0 not correct for eval'
134
fi
135
$SHELL -c 'read x <<< hello' 2> /dev/null || err_exit 'syntax <<< not recognized'
136
($SHELL -c 'read x[1] <<< hello') 2> /dev/null || err_exit 'read x[1] not working'
137
unset x
138
readonly x
139
set -- $(readonly)
140
if [[ " $@ " != *" x "* ]]
141
then err_exit 'unset readonly variables are not displayed'
142
fi
143
if [[ $( for i in foo bar
144
do print $i
145
continue 10
146
done
147
) != $'foo\nbar' ]]
148
then err_exit 'continue breaks out of loop'
149
fi
150
(continue bad 2>/dev/null && err_exit 'continue bad should return an error')
151
(break bad 2>/dev/null && err_exit 'break bad should return an error')
152
(continue 0 2>/dev/null && err_exit 'continue 0 should return an error')
153
(break 0 2>/dev/null && err_exit 'break 0 should return an error')
154
breakfun() { break;}
155
continuefun() { continue;}
156
for fun in break continue
157
do if [[ $( for i in foo
158
do ${fun}fun
159
print $i
160
done
161
) != foo ]]
162
then err_exit "$fun call in ${fun}fun breaks out of for loop"
163
fi
164
done
165
if [[ $(print -f "%b" "\a\n\v\b\r\f\E\03\\oo") != $'\a\n\v\b\r\f\E\03\\oo' ]]
166
then err_exit 'print -f "%b" not working'
167
fi
168
if [[ $(print -f "%P" "[^x].*b\$") != '*[!x]*b' ]]
169
then err_exit 'print -f "%P" not working'
170
fi
171
if [[ $(print -f "%(pattern)q" "[^x].*b\$") != '*[!x]*b' ]]
172
then err_exit 'print -f "%(pattern)q" not working'
173
fi
174
if [[ $(abc: for i in foo bar;do print $i;break abc;done) != foo ]]
175
then err_exit 'break labels not working'
176
fi
177
if [[ $(command -v if) != if ]]
178
then err_exit 'command -v not working'
179
fi
180
read -r var <<\!
181
182
!
183
if [[ $var != "" ]]
184
then err_exit "read -r of blank line not working"
185
fi
186
mkdir -p $tmp/a/b/c 2>/dev/null || err_exit "mkdir -p failed"
187
$SHELL -c "cd $tmp/a/b; cd c" 2>/dev/null || err_exit "initial script relative cd fails"
188
189
trap 'print TERM' TERM
190
exp=$'trap -- \'print TERM\' TERM\ntrap -- \'cd /; rm -rf '$tmp$'\' EXIT'
191
got=$(trap)
192
[[ $got == $exp ]] || err_exit "\$(trap) failed -- expected \"$exp\", got \"$got\""
193
exp='print TERM'
194
got=$(trap -p TERM)
195
[[ $got == $exp ]] || err_exit "\$(trap -p TERM) failed -- expected \"$exp\", got \"$got\""
196
197
[[ $($SHELL -c 'trap "print ok" SIGTERM; kill -s SIGTERM $$' 2> /dev/null) == ok ]] || err_exit 'SIGTERM not recognized'
198
[[ $($SHELL -c 'trap "print ok" sigterm; kill -s sigterm $$' 2> /dev/null) == ok ]] || err_exit 'SIGTERM not recognized'
199
[[ $($SHELL -c '( trap "" TERM);kill $$;print bad' == bad) ]] 2> /dev/null && err_exit 'trap ignored in subshell causes it to be ignored by parent'
200
${SHELL} -c 'kill -1 -$$' 2> /dev/null
201
[[ $(kill -l $?) == HUP ]] || err_exit 'kill -1 -pid not working'
202
${SHELL} -c 'kill -1 -$$' 2> /dev/null
203
[[ $(kill -l $?) == HUP ]] || err_exit 'kill -n1 -pid not working'
204
${SHELL} -c 'kill -s HUP -$$' 2> /dev/null
205
[[ $(kill -l $?) == HUP ]] || err_exit 'kill -HUP -pid not working'
206
n=123
207
typeset -A base
208
base[o]=8#
209
base[x]=16#
210
base[X]=16#
211
for i in d i o u x X
212
do if (( $(( ${base[$i]}$(printf "%$i" $n) )) != n ))
213
then err_exit "printf %$i not working"
214
fi
215
done
216
if [[ $( trap 'print done' EXIT) != done ]]
217
then err_exit 'trap on EXIT not working'
218
fi
219
if [[ $( trap 'print done' EXIT; trap - EXIT) == done ]]
220
then err_exit 'trap on EXIT not being cleared'
221
fi
222
if [[ $(LC_MESSAGES=C type test) != 'test is a shell builtin' ]]
223
then err_exit 'whence -v test not a builtin'
224
fi
225
builtin -d test
226
if [[ $(type test) == *builtin* ]]
227
then err_exit 'whence -v test after builtin -d incorrect'
228
fi
229
typeset -Z3 percent=$(printf '%o\n' "'%'")
230
forrmat=\\${percent}s
231
if [[ $(printf "$forrmat") != %s ]]
232
then err_exit "printf $forrmat not working"
233
fi
234
if (( $(printf 'x\0y' | wc -c) != 3 ))
235
then err_exit 'printf \0 not working'
236
fi
237
if [[ $(printf "%bx%s\n" 'f\to\cbar') != $'f\to' ]]
238
then err_exit 'printf %bx%s\n not working'
239
fi
240
alpha=abcdefghijklmnop
241
if [[ $(printf "%10.*s\n" 5 $alpha) != ' abcde' ]]
242
then err_exit 'printf %10.%s\n not working'
243
fi
244
float x2=.0000625
245
if [[ $(printf "%10.5E\n" x2) != 6.25000E-05 ]]
246
then err_exit 'printf "%10.5E" not normalizing correctly'
247
fi
248
x2=.000000001
249
if [[ $(printf "%g\n" x2 2>/dev/null) != 1e-09 ]]
250
then err_exit 'printf "%g" not working correctly'
251
fi
252
#FIXME#($SHELL read -s foobar <<\!
253
#FIXME#testing
254
#FIXME#!
255
#FIXME#) 2> /dev/null || err_exit ksh read -s var fails
256
if [[ $(printf +3 2>/dev/null) != +3 ]]
257
then err_exit 'printf is not processing formats beginning with + correctly'
258
fi
259
if printf "%d %d\n" 123bad 78 >/dev/null 2>/dev/null
260
then err_exit "printf not exiting non-zero with conversion errors"
261
fi
262
if [[ $(trap --version 2> /dev/null;print done) != done ]]
263
then err_exit 'trap builtin terminating after --version'
264
fi
265
if [[ $(set --version 2> /dev/null;print done) != done ]]
266
then err_exit 'set builtin terminating after --veresion'
267
fi
268
unset -f foobar
269
function foobar
270
{
271
print 'hello world'
272
}
273
OPTIND=1
274
if [[ $(getopts $'[+?X\ffoobar\fX]' v --man 2>&1) != *'Xhello world'X* ]]
275
then err_exit '\f...\f not working in getopts usage strings'
276
fi
277
if [[ $(printf '%H\n' $'<>"& \'\tabc') != '&lt;&gt;&quot;&amp;&nbsp;&apos;&#9;abc' ]]
278
then err_exit 'printf %H not working'
279
fi
280
if [[ $(printf '%(html)q\n' $'<>"& \'\tabc') != '&lt;&gt;&quot;&amp;&nbsp;&apos;&#9;abc' ]]
281
then err_exit 'printf %(html)q not working'
282
fi
283
if [[ $( printf 'foo://ab_c%(url)q\n' $'<>"& \'\tabc') != 'foo://ab_c%3C%3E%22%26%20%27%09abc' ]]
284
then err_exit 'printf %(url)q not working'
285
fi
286
if [[ $(printf '%R %R %R %R\n' 'a.b' '*.c' '^' '!(*.*)') != '^a\.b$ \.c$ ^\^$ ^(.*\..*)!$' ]]
287
then err_exit 'printf %T not working'
288
fi
289
if [[ $(printf '%(ere)q %(ere)q %(ere)q %(ere)q\n' 'a.b' '*.c' '^' '!(*.*)') != '^a\.b$ \.c$ ^\^$ ^(.*\..*)!$' ]]
290
then err_exit 'printf %(ere)q not working'
291
fi
292
if [[ $(printf '%..:c\n' abc) != a:b:c ]]
293
then err_exit "printf '%..:c' not working"
294
fi
295
if [[ $(printf '%..*c\n' : abc) != a:b:c ]]
296
then err_exit "printf '%..*c' not working"
297
fi
298
if [[ $(printf '%..:s\n' abc def ) != abc:def ]]
299
then err_exit "printf '%..:s' not working"
300
fi
301
if [[ $(printf '%..*s\n' : abc def) != abc:def ]]
302
then err_exit "printf '%..*s' not working"
303
fi
304
[[ $(printf '%q\n') == '' ]] || err_exit 'printf "%q" with missing arguments'
305
# we won't get hit by the one second boundary twice, right?
306
[[ $(printf '%T\n' now) == "$(date)" ]] ||
307
[[ $(printf '%T\n' now) == "$(date)" ]] ||
308
err_exit 'printf "%T" now'
309
behead()
310
{
311
read line
312
left=$(cat)
313
}
314
print $'line1\nline2' | behead
315
if [[ $left != line2 ]]
316
then err_exit "read reading ahead on a pipe"
317
fi
318
read -n1 y <<!
319
abc
320
!
321
exp=a
322
if [[ $y != $exp ]]
323
then err_exit "read -n1 failed -- expected '$exp', got '$y'"
324
fi
325
print -n $'{ read -r line;print $line;}\nhello' > $tmp/script
326
chmod 755 $tmp/script
327
if [[ $($SHELL < $tmp/script) != hello ]]
328
then err_exit 'read of incomplete line not working correctly'
329
fi
330
set -f
331
set -- *
332
if [[ $1 != '*' ]]
333
then err_exit 'set -f not working'
334
fi
335
unset pid1 pid2
336
false &
337
pid1=$!
338
pid2=$(
339
wait $pid1
340
(( $? == 127 )) || err_exit "job known to subshell"
341
print $!
342
)
343
wait $pid1
344
(( $? == 1 )) || err_exit "wait not saving exit value"
345
wait $pid2
346
(( $? == 127 )) || err_exit "subshell job known to parent"
347
env=
348
v=$(getconf LIBPATH)
349
for v in ${v//,/ }
350
do v=${v#*:}
351
v=${v%%:*}
352
eval [[ \$$v ]] && env="$env $v=\"\$$v\""
353
done
354
if [[ $(foo=bar; eval foo=\$foo $env exec -c \$SHELL -c \'print \$foo\') != bar ]]
355
then err_exit '"name=value exec -c ..." not working'
356
fi
357
$SHELL -c 'OPTIND=-1000000; getopts a opt -a' 2> /dev/null
358
[[ $? == 1 ]] || err_exit 'getopts with negative OPTIND not working'
359
getopts 'n#num' opt -n 3
360
[[ $OPTARG == 3 ]] || err_exit 'getopts with numerical arguments failed'
361
if [[ $($SHELL -c $'printf \'%2$s %1$s\n\' world hello') != 'hello world' ]]
362
then err_exit 'printf %2$s %1$s not working'
363
fi
364
val=$(( 'C' ))
365
set -- \
366
"'C" $val 0 \
367
"'C'" $val 0 \
368
'"C' $val 0 \
369
'"C"' $val 0 \
370
"'CX" $val 1 \
371
"'CX'" $val 1 \
372
"'C'X" $val 1 \
373
'"CX' $val 1 \
374
'"CX"' $val 1 \
375
'"C"X' $val 1
376
while (( $# >= 3 ))
377
do arg=$1 val=$2 code=$3
378
shift 3
379
for fmt in '%d' '%g'
380
do out=$(printf "$fmt" "$arg" 2>/dev/null)
381
err=$(printf "$fmt" "$arg" 2>&1 >/dev/null)
382
printf "$fmt" "$arg" >/dev/null 2>&1
383
ret=$?
384
[[ $out == $val ]] || err_exit "printf $fmt $arg failed -- expected '$val', got '$out'"
385
if (( $code ))
386
then [[ $err ]] || err_exit "printf $fmt $arg failed, error message expected"
387
else [[ $err ]] && err_exit "$err: printf $fmt $arg failed, error message not expected -- got '$err'"
388
fi
389
(( $ret == $code )) || err_exit "printf $fmt $arg failed -- expected exit code $code, got $ret"
390
done
391
done
392
((n=0))
393
((n++)); ARGC[$n]=1 ARGV[$n]=""
394
((n++)); ARGC[$n]=2 ARGV[$n]="-a"
395
((n++)); ARGC[$n]=4 ARGV[$n]="-a -v 2"
396
((n++)); ARGC[$n]=4 ARGV[$n]="-a -v 2 x"
397
((n++)); ARGC[$n]=4 ARGV[$n]="-a -v 2 x y"
398
for ((i=1; i<=n; i++))
399
do set -- ${ARGV[$i]}
400
OPTIND=0
401
while getopts -a tst "av:" OPT
402
do :
403
done
404
if [[ $OPTIND != ${ARGC[$i]} ]]
405
then err_exit "\$OPTIND after getopts loop incorrect -- expected ${ARGC[$i]}, got $OPTIND"
406
fi
407
done
408
options=ab:c
409
optarg=foo
410
set -- -a -b $optarg -c bar
411
while getopts $options opt
412
do case $opt in
413
a|c) [[ $OPTARG ]] && err_exit "getopts $options \$OPTARG for flag $opt failed, expected \"\", got \"$OPTARG\"" ;;
414
b) [[ $OPTARG == $optarg ]] || err_exit "getopts $options \$OPTARG failed -- \"$optarg\" expected, got \"$OPTARG\"" ;;
415
*) err_exit "getopts $options failed -- got flag $opt" ;;
416
esac
417
done
418
419
[[ $($SHELL 2> /dev/null -c 'readonly foo; getopts a: foo -a blah; echo foo') == foo ]] || err_exit 'getopts with readonly variable causes script to abort'
420
421
unset a
422
{ read -N3 a; read -N1 b;} <<!
423
abcdefg
424
!
425
exp=abc
426
[[ $a == $exp ]] || err_exit "read -N3 here-document failed -- expected '$exp', got '$a'"
427
exp=d
428
[[ $b == $exp ]] || err_exit "read -N1 here-document failed -- expected '$exp', got '$b'"
429
read -n3 a <<!
430
abcdefg
431
!
432
exp=abc
433
[[ $a == $exp ]] || err_exit "read -n3 here-document failed -- expected '$exp', got '$a'"
434
#(print -n a;sleep 1; print -n bcde) | { read -N3 a; read -N1 b;}
435
#[[ $a == $exp ]] || err_exit "read -N3 from pipe failed -- expected '$exp', got '$a'"
436
#exp=d
437
#[[ $b == $exp ]] || err_exit "read -N1 from pipe failed -- expected '$exp', got '$b'"
438
#(print -n a;sleep 1; print -n bcde) | read -n3 a
439
#exp=a
440
#[[ $a == $exp ]] || err_exit "read -n3 from pipe failed -- expected '$exp', got '$a'"
441
#rm -f $tmp/fifo
442
#if mkfifo $tmp/fifo 2> /dev/null
443
#then (print -n a; sleep 1;print -n bcde) > $tmp/fifo &
444
# {
445
# read -u5 -n3 -t2 a || err_exit 'read -n3 from fifo timedout'
446
# read -u5 -n1 -t2 b || err_exit 'read -n1 from fifo timedout'
447
# } 5< $tmp/fifo
448
# exp=a
449
# [[ $a == $exp ]] || err_exit "read -n3 from fifo failed -- expected '$exp', got '$a'"
450
# rm -f $tmp/fifo
451
# mkfifo $tmp/fifo 2> /dev/null
452
# (print -n a; sleep 1;print -n bcde) > $tmp/fifo &
453
# {
454
# read -u5 -N3 -t2 a || err_exit 'read -N3 from fifo timed out'
455
# read -u5 -N1 -t2 b || err_exit 'read -N1 from fifo timedout'
456
# } 5< $tmp/fifo
457
# exp=abc
458
# [[ $a == $exp ]] || err_exit "read -N3 from fifo failed -- expected '$exp', got '$a'"
459
# exp=d
460
# [[ $b == $exp ]] || err_exit "read -N1 from fifo failed -- expected '$exp', got '$b'"
461
#fi
462
#rm -f $tmp/fifo
463
464
function longline
465
{
466
integer i
467
for((i=0; i < $1; i++))
468
do print argument$i
469
done
470
}
471
# test command -x option
472
integer sum=0 n=10000
473
if ! ${SHELL:-ksh} -c 'print $#' count $(longline $n) > /dev/null 2>&1
474
then for i in $(command command -x ${SHELL:-ksh} -c 'print $#;[[ $1 != argument0 ]]' count $(longline $n) 2> /dev/null)
475
do ((sum += $i))
476
done
477
(( sum == n )) || err_exit "command -x processed only $sum arguments"
478
command -p command -x ${SHELL:-ksh} -c 'print $#;[[ $1 == argument0 ]]' count $(longline $n) > /dev/null 2>&1
479
[[ $? != 1 ]] && err_exit 'incorrect exit status for command -x'
480
fi
481
# test command -x option with extra arguments
482
integer sum=0 n=10000
483
if ! ${SHELL:-ksh} -c 'print $#' count $(longline $n) > /dev/null 2>&1
484
then for i in $(command command -x ${SHELL:-ksh} -c 'print $#;[[ $1 != argument0 ]]' count $(longline $n) one two three) #2> /dev/null)
485
do ((sum += $i))
486
done
487
(( sum > n )) || err_exit "command -x processed only $sum arguments"
488
(( (sum-n)%3==0 )) || err_exit "command -x processed only $sum arguments"
489
(( sum == n+3)) && err_exit "command -x processed only $sum arguments"
490
command -p command -x ${SHELL:-ksh} -c 'print $#;[[ $1 == argument0 ]]' count $(longline $n) > /dev/null 2>&1
491
[[ $? != 1 ]] && err_exit 'incorrect exit status for command -x'
492
fi
493
# test for debug trap
494
[[ $(typeset -i i=0
495
trap 'print $i' DEBUG
496
while (( i <2))
497
do (( i++))
498
done) == $'0\n0\n1\n1\n2' ]] || err_exit "DEBUG trap not working"
499
getconf UNIVERSE - ucb
500
[[ $($SHELL -c 'echo -3') == -3 ]] || err_exit "echo -3 not working in ucb universe"
501
typeset -F3 start_x=SECONDS total_t delay=0.02
502
typeset reps=50 leeway=5
503
sleep $(( 2 * leeway * reps * delay )) |
504
for (( i=0 ; i < reps ; i++ ))
505
do read -N1 -t $delay
506
done
507
(( total_t = SECONDS - start_x ))
508
if (( total_t > leeway * reps * delay ))
509
then err_exit "read -t in pipe taking $total_t secs - $(( reps * delay )) minimum - too long"
510
elif (( total_t < reps * delay ))
511
then err_exit "read -t in pipe taking $total_t secs - $(( reps * delay )) minimum - too fast"
512
fi
513
$SHELL -c 'sleep $(printf "%a" .95)' 2> /dev/null || err_exit "sleep doesn't except %a format constants"
514
$SHELL -c 'test \( ! -e \)' 2> /dev/null ; [[ $? == 1 ]] || err_exit 'test \( ! -e \) not working'
515
[[ $(ulimit) == "$(ulimit -fS)" ]] || err_exit 'ulimit is not the same as ulimit -fS'
516
tmpfile=$tmp/file.2
517
print $'\nprint -r -- "${.sh.file} ${LINENO} ${.sh.lineno}"' > $tmpfile
518
[[ $( . "$tmpfile") == "$tmpfile 2 1" ]] || err_exit 'dot command not working'
519
print -r -- "'xxx" > $tmpfile
520
[[ $($SHELL -c ". $tmpfile"$'\n print ok' 2> /dev/null) == ok ]] || err_exit 'syntax error in dot command affects next command'
521
522
float sec=$SECONDS del=4
523
exec 3>&2 2>/dev/null
524
$SHELL -c "( sleep 1; kill -ALRM \$\$ ) & sleep $del" 2> /dev/null
525
exitval=$?
526
(( sec = SECONDS - sec ))
527
exec 2>&3-
528
(( exitval )) && err_exit "sleep doesn't exit 0 with ALRM interupt"
529
(( sec > (del - 1) )) || err_exit "ALRM signal causes sleep to terminate prematurely -- expected 3 sec, got $sec"
530
typeset -r z=3
531
y=5
532
for i in 123 z %x a.b.c
533
do ( unset $i) 2>/dev/null && err_exit "unset $i should fail"
534
done
535
a=()
536
for i in y y y[8] t[abc] y.d a.b a
537
do unset $i || print -u2 "err_exit unset $i should not fail"
538
done
539
[[ $($SHELL -c 'y=3; unset 123 y;print $?$y') == 1 ]] 2> /dev/null || err_exit 'y is not getting unset with unset 123 y'
540
[[ $($SHELL -c 'trap foo TERM; (trap;(trap) )') == 'trap -- foo TERM' ]] || err_exit 'traps not getting reset when subshell is last process'
541
542
n=$(printf "%b" 'a\0b\0c' | wc -c)
543
(( n == 5 )) || err_exit '\0 not working with %b format with printf'
544
545
t=$(ulimit -t)
546
[[ $($SHELL -c 'ulimit -v 15000 2>/dev/null; ulimit -t') == "$t" ]] || err_exit 'ulimit -v changes ulimit -t'
547
548
$SHELL 2> /dev/null -c 'cd ""' && err_exit 'cd "" not producing an error'
549
[[ $($SHELL 2> /dev/null -c 'cd "";print hi') != hi ]] && err_exit 'cd "" should not terminate script'
550
551
bincat=$(whence -p cat)
552
builtin cat
553
out=$tmp/seq.out
554
seq 11 >$out
555
cmp -s <(print -- "$($bincat<( $bincat $out ) )") <(print -- "$(cat <( cat $out ) )") || err_exit "builtin cat differs from $bincat"
556
557
[[ $($SHELL -c '{ printf %R "["; print ok;}' 2> /dev/null) == ok ]] || err_exit $'\'printf %R "["\' causes shell to abort'
558
559
v=$( $SHELL -c $'
560
trap \'print "usr1"\' USR1
561
trap exit USR2
562
sleep 1 && {
563
kill -USR1 $$ && sleep 1
564
kill -0 $$ 2>/dev/null && kill -USR2 $$
565
} &
566
sleep 2 | read
567
echo done
568
' ) 2> /dev/null
569
[[ $v == $'usr1\ndone' ]] || err_exit 'read not terminating when receiving USR1 signal'
570
571
mkdir $tmp/tmpdir1
572
cd $tmp/tmpdir1
573
pwd=$PWD
574
cd ../tmpdir1
575
[[ $PWD == "$pwd" ]] || err_exit 'cd ../tmpdir1 causes directory to change'
576
cd "$pwd"
577
mv $tmp/tmpdir1 $tmp/tmpdir2
578
cd .. 2> /dev/null || err_exit 'cannot change directory to .. after current directory has been renamed'
579
[[ $PWD == "$tmp" ]] || err_exit 'after "cd $tmp/tmpdir1; cd .." directory is not $tmp'
580
581
cd "$tmp"
582
mkdir $tmp/tmpdir2/foo
583
pwd=$PWD
584
cd $tmp/tmpdir2/foo
585
mv $tmp/tmpdir2 $tmp/tmpdir1
586
cd ../.. 2> /dev/null || err_exit 'cannot change directory to ../.. after current directory has been renamed'
587
[[ $PWD == "$tmp" ]] || err_exit 'after "cd $tmp/tmpdir2; cd ../.." directory is not $tmp'
588
cd "$tmp"
589
rm -rf tmpdir1
590
591
cd /etc
592
cd ..
593
[[ $(pwd) == / ]] || err_exit 'cd /etc;cd ..;pwd is not /'
594
cd /etc
595
cd ../..
596
[[ $(pwd) == / ]] || err_exit 'cd /etc;cd ../..;pwd is not /'
597
cd /etc
598
cd .././..
599
[[ $(pwd) == / ]] || err_exit 'cd /etc;cd .././..;pwd is not /'
600
cd /usr/bin
601
cd ../..
602
[[ $(pwd) == / ]] || err_exit 'cd /usr/bin;cd ../..;pwd is not /'
603
cd /usr/bin
604
cd ..
605
[[ $(pwd) == /usr ]] || err_exit 'cd /usr/bin;cd ..;pwd is not /usr'
606
cd "$tmp"
607
if mkdir $tmp/t1
608
then (
609
cd $tmp/t1
610
> real_t1
611
(
612
cd ..
613
mv t1 t2
614
mkdir t1
615
)
616
[[ -f real_t1 ]] || err_exit 'real_t1 not found after parent directory renamed in subshell'
617
)
618
fi
619
cd "$tmp"
620
621
$SHELL +E -i <<- \! && err_exit 'interactive shell should not exit 0 after false'
622
false
623
exit
624
!
625
626
if kill -L > /dev/null 2>&1
627
then [[ $(kill -l HUP) == "$(kill -L HUP)" ]] || err_exit 'kill -l and kill -L are not the same when given a signal name'
628
[[ $(kill -l 9) == "$(kill -L 9)" ]] || err_exit 'kill -l and kill -L are not the same when given a signal number'
629
[[ $(kill -L) == *'9) KILL'* ]] || err_exit 'kill -L output does not contain 9) KILL'
630
fi
631
632
unset ENV
633
v=$($SHELL 2> /dev/null +o rc -ic $'getopts a:bc: opt --man\nprint $?')
634
[[ $v == 2* ]] || err_exit 'getopts --man does not exit 2 for interactive shells'
635
636
read baz <<< 'foo\\\\bar'
637
[[ $baz == 'foo\\bar' ]] || err_exit 'read of foo\\\\bar not getting foo\\bar'
638
639
: ~root
640
[[ $(builtin) == *.sh.tilde* ]] && err_exit 'builtin contains .sh.tilde'
641
642
exit $((Errors<125?Errors:125))
643
644