Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/ksh93/tests/bracket.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
null=''
35
if [[ ! -z $null ]]
36
then err_exit "-z: null string should be of zero length"
37
fi
38
file=$tmp/original
39
newer_file=$tmp/newer
40
if [[ -z $file ]]
41
then err_exit "-z: $file string should not be of zero length"
42
fi
43
if [[ -a $file ]]
44
then err_exit "-a: $file shouldn't exist"
45
fi
46
if [[ -e $file ]]
47
then err_exit "-e: $file shouldn't exist"
48
fi
49
> $file
50
if [[ ! -a $file ]]
51
then err_exit "-a: $file should exist"
52
fi
53
if [[ ! -e $file ]]
54
then err_exit "-e: $file should exist"
55
fi
56
chmod 777 $file
57
if [[ ! -r $file ]]
58
then err_exit "-r: $file should be readable"
59
fi
60
if [[ ! -w $file ]]
61
then err_exit "-w: $file should be writable"
62
fi
63
if [[ ! -w $file ]]
64
then err_exit "-x: $file should be executable"
65
fi
66
if [[ ! -w $file || ! -r $file ]]
67
then err_exit "-rw: $file should be readable/writable"
68
fi
69
if [[ -s $file ]]
70
then err_exit "-s: $file should be of zero size"
71
fi
72
if [[ ! -f $file ]]
73
then err_exit "-f: $file should be an ordinary file"
74
fi
75
if [[ -d $file ]]
76
then err_exit "-f: $file should not be a directory file"
77
fi
78
if [[ ! -d . ]]
79
then err_exit "-d: . should not be a directory file"
80
fi
81
if [[ -f /dev/null ]]
82
then err_exit "-f: /dev/null should not be an ordinary file"
83
fi
84
chmod 000 $file
85
if [[ -r $file ]]
86
then err_exit "-r: $file should not be readable"
87
fi
88
if [[ ! -O $file ]]
89
then err_exit "-r: $file should be owned by me"
90
fi
91
if [[ -w $file ]]
92
then err_exit "-w: $file should not be writable"
93
fi
94
if [[ -w $file ]]
95
then err_exit "-x: $file should not be executable"
96
fi
97
if [[ -w $file || -r $file ]]
98
then err_exit "-rw: $file should not be readable/writable"
99
fi
100
if [[ -z x && -z x || ! -z x ]]
101
then :
102
else err_exit " wrong precedence"
103
fi
104
if [[ -z x && (-z x || ! -z x) ]]
105
then err_exit " () grouping not working"
106
fi
107
if [[ foo < bar ]]
108
then err_exit "foo comes before bar"
109
fi
110
[[ . -ef $(pwd) ]] || err_exit ". is not $PWD"
111
set -o allexport
112
[[ -o allexport ]] || err_exit '-o: did not set allexport option'
113
if [[ -n $null ]]
114
then err_exit "'$null' has non-zero length"
115
fi
116
if [[ ! -r /dev/fd/0 ]]
117
then err_exit "/dev/fd/0 not open for reading"
118
fi
119
if [[ ! -w /dev/fd/2 ]]
120
then err_exit "/dev/fd/2 not open for writing"
121
fi
122
sleep 1
123
> $newer_file
124
if [[ ! $file -ot $newer_file ]]
125
then err_exit "$file should be older than $newer_file"
126
fi
127
if [[ $file -nt $newer_file ]]
128
then err_exit "$newer_file should be newer than $file"
129
fi
130
if [[ $file != $tmp/* ]]
131
then err_exit "$file should match $tmp/*"
132
fi
133
if [[ $file == $tmp'/*' ]]
134
then err_exit "$file should not equal $tmp'/*'"
135
fi
136
[[ ! ( ! -z $null && ! -z x) ]] || err_exit "negation and grouping"
137
[[ -z '' || -z '' || -z '' ]] || err_exit "three ors not working"
138
[[ -z '' && -z '' && -z '' ]] || err_exit "three ors not working"
139
(exit 8)
140
if [[ $? -ne 8 || $? -ne 8 ]]
141
then err_exit 'value $? within [[...]]'
142
fi
143
x='(x'
144
if [[ '(x' != '('* ]]
145
then err_exit " '(x' does not match '('* within [[...]]"
146
fi
147
if [[ '(x' != "("* ]]
148
then err_exit ' "(x" does not match "("* within [[...]]'
149
fi
150
if [[ '(x' != \(* ]]
151
then err_exit ' "(x" does not match \(* within [[...]]'
152
fi
153
if [[ 'x(' != *'(' ]]
154
then err_exit " 'x(' does not match '('* within [[...]]"
155
fi
156
if [[ 'x&' != *'&' ]]
157
then err_exit " 'x&' does not match '&'* within [[...]]"
158
fi
159
if [[ 'xy' == *'*' ]]
160
then err_exit " 'xy' matches *'*' within [[...]]"
161
fi
162
if [[ 3 > 4 ]]
163
then err_exit '3 < 4'
164
fi
165
if [[ 4 < 3 ]]
166
then err_exit '3 > 4'
167
fi
168
if [[ 3x > 4x ]]
169
then err_exit '3x < 4x'
170
fi
171
x='@(bin|dev|?)'
172
cd /
173
if [[ $(print $x) != "$x" ]]
174
then err_exit 'extended pattern matching on command arguments'
175
fi
176
if [[ dev != $x ]]
177
then err_exit 'extended pattern matching not working on variables'
178
fi
179
if [[ -u $SHELL ]]
180
then err_exit "setuid on $SHELL"
181
fi
182
if [[ -g $SHELL ]]
183
then err_exit "setgid on $SHELL"
184
fi
185
test -d . -a '(' ! -f . ')' || err_exit 'test not working'
186
if [[ '!' != ! ]]
187
then err_exit 'quoting unary operator not working'
188
fi
189
test \( -n x \) -o \( -n y \) 2> /dev/null || err_exit 'test ( -n x ) -o ( -n y) not working'
190
test \( -n x \) -o -n y 2> /dev/null || err_exit 'test ( -n x ) -o -n y not working'
191
chmod 600 $file
192
exec 4> $file
193
print -u4 foobar
194
if [[ ! -s $file ]]
195
then err_exit "-s: $file should be non-zero"
196
fi
197
exec 4>&-
198
if [[ 011 -ne 11 ]]
199
then err_exit "leading zeros in arithmetic compares not ignored"
200
fi
201
{
202
set -x
203
[[ foo > bar ]]
204
} 2> /dev/null || { set +x; err_exit "foo<bar with -x enabled" ;}
205
set +x
206
(
207
eval "[[ (a) ]]"
208
) 2> /dev/null || err_exit "[[ (a) ]] not working"
209
> $file
210
chmod 4755 "$file"
211
if test -u $file && test ! -u $file
212
then err_exit "test ! -u suidfile not working"
213
fi
214
for i in '(' ')' '[' ']'
215
do [[ $i == $i ]] || err_exit "[[ $i != $i ]]"
216
done
217
(
218
[[ aaaa == {4}(a) ]] || err_exit 'aaaa != {4}(a)'
219
[[ aaaa == {2,5}(a) ]] || err_exit 'aaaa != {2,4}(a)'
220
[[ abcdcdabcd == {3,6}(ab|cd) ]] || err_exit 'abcdcdabcd == {3,4}(ab|cd)'
221
[[ abcdcdabcde == {5}(ab|cd)e ]] || err_exit 'abcdcdabcd == {5}(ab|cd)e'
222
) || err_exit 'errors with {..}(...) patterns'
223
[[ D290.2003.02.16.temp == D290.+(2003.02.16).temp* ]] || err_exit 'pattern match bug with +(...)'
224
rm -rf $file
225
{
226
[[ -N $file ]] && err_exit 'test -N $tmp/*: st_mtime>st_atime after creat'
227
sleep 2
228
print 'hello world'
229
[[ -N $file ]] || err_exit 'test -N $tmp/*: st_mtime<=st_atime after write'
230
sleep 2
231
read
232
[[ -N $file ]] && err_exit 'test -N $tmp/*: st_mtime>st_atime after read'
233
} > $file < $file
234
if rm -rf "$file" && ln -s / "$file"
235
then [[ -L "$file" ]] || err_exit '-L not working'
236
[[ -L "$file"/ ]] && err_exit '-L with file/ not working'
237
fi
238
$SHELL -c 't=1234567890; [[ $t == @({10}(\d)) ]]' 2> /dev/null || err_exit ' @({10}(\d)) pattern not working'
239
$SHELL -c '[[ att_ == ~(E)(att|cus)_.* ]]' 2> /dev/null || err_exit ' ~(E)(att|cus)_* pattern not working'
240
$SHELL -c '[[ att_ =~ (att|cus)_.* ]]' 2> /dev/null || err_exit ' =~ ere not working'
241
$SHELL -c '[[ abc =~ a(b)c ]]' 2> /dev/null || err_exit '[[ abc =~ a(b)c ]] fails'
242
$SHELL -xc '[[ abc =~ \babc\b ]]' 2> /dev/null || err_exit '[[ abc =~ \babc\b ]] fails'
243
[[ abc == ~(E)\babc\b ]] || err_exit '\b not preserved for ere when not in ()'
244
[[ abc == ~(iEi)\babc\b ]] || err_exit '\b not preserved for ~(iEi) when not in ()'
245
246
e=$($SHELL -c '[ -z "" -a -z "" ]' 2>&1)
247
[[ $e ]] && err_exit "[ ... ] compatibility check failed -- $e"
248
i=hell
249
[[ hell0 == $i[0] ]] || err_exit 'pattern $i[0] interpreded as array ref'
250
test '(' = ')' && err_exit '"test ( = )" should not be true'
251
[[ $($SHELL -c 'case F in ~(Eilr)[a-z0-9#]) print ok;;esac' 2> /dev/null) == ok ]] || err_exit '~(Eilr) not working in case command'
252
[[ $($SHELL -c "case Q in ~(Fi)q | \$'\E') print ok;;esac" 2> /dev/null) == ok ]] || err_exit '~(Fi)q | \E not working in case command'
253
254
for l in C en_US.ISO8859-15
255
do [[ $($SHELL -c "LC_COLLATE=$l" 2>&1) ]] && continue
256
export LC_COLLATE=$l
257
set -- \
258
'A' 0 1 1 0 1 1 1 0 0 1 0 0 \
259
'Z' 0 1 1 0 1 1 1 0 0 1 0 0 \
260
'/' 0 0 0 0 0 0 1 1 1 1 1 1 \
261
'.' 0 0 0 0 0 0 1 1 1 1 1 1 \
262
'_' 0 0 0 0 0 0 1 1 1 1 1 1 \
263
'-' 1 1 1 1 1 1 0 0 0 0 0 0 \
264
'%' 0 0 0 0 0 0 1 1 1 1 1 1 \
265
'@' 0 0 0 0 0 0 1 1 1 1 1 1 \
266
'!' 0 0 0 0 0 0 1 1 1 1 1 1 \
267
'^' 0 0 0 0 0 0 1 1 1 1 1 1 \
268
# retain this line #
269
while (( $# >= 13 ))
270
do c=$1
271
shift
272
for p in \
273
'[![.-.]]' \
274
'[![.-.][:upper:]]' \
275
'[![.-.]A-Z]' \
276
'[!-]' \
277
'[!-[:upper:]]' \
278
'[!-A-Z]' \
279
'[[.-.]]' \
280
'[[.-.][:upper:]]' \
281
'[[.-.]A-Z]' \
282
'[-]' \
283
'[-[:upper:]]' \
284
'[-A-Z]' \
285
# retain this line #
286
do e=$1
287
shift
288
[[ $c == $p ]]
289
g=$?
290
[[ $g == $e ]] || err_exit "[[ '$c' == $p ]] for LC_COLLATE=$l failed -- expected $e, got $g"
291
done
292
done
293
done
294
integer n
295
if ( : < /dev/tty ) 2>/dev/null && exec {n}< /dev/tty
296
then [[ -t $n ]] || err_exit "[[ -t n ]] fails when n > 9"
297
fi
298
foo=([1]=a [2]=b [3]=c)
299
[[ -v foo[1] ]] || err_exit 'foo[1] should be set'
300
[[ ${foo[1]+x} ]] || err_exit '${foo[1]+x} should be x'
301
[[ ${foo[@]+x} ]] || err_exit '${foo[@]+x} should be x'
302
unset foo[1]
303
[[ -v foo[1] ]] && err_exit 'foo[1] should not be set'
304
[[ ${foo[1]+x} ]] && err_exit '${foo[1]+x} should be empty'
305
bar=(a b c)
306
[[ -v bar[1] ]] || err_exit 'bar[1] should be set'
307
[[ ${bar[1]+x} ]] || err_exit '${foo[1]+x} should be x'
308
unset bar[1]
309
[[ ${bar[1]+x} ]] && err_exit '${foo[1]+x} should be empty'
310
[[ -v bar ]] || err_exit 'bar should be set'
311
[[ -v bar[1] ]] && err_exit 'bar[1] should not be set'
312
integer z=( 1 2 4)
313
[[ -v z[1] ]] || err_exit 'z[1] should be set'
314
unset z[1]
315
[[ -v z[1] ]] && err_exit 'z[1] should not be set'
316
typeset -si y=( 1 2 4)
317
[[ -v y[6] ]] && err_exit 'y[6] should not be set'
318
[[ -v y[1] ]] || err_exit 'y[1] should be set'
319
unset y[1]
320
[[ -v y[1] ]] && err_exit 'y[1] should not be set'
321
x=abc
322
[[ -v x[0] ]] || err_exit 'x[0] should be set'
323
[[ ${x[0]+x} ]] || err_exit print '${x[0]+x} should be x'
324
[[ -v x[3] ]] && err_exit 'x[3] should not be set'
325
[[ ${x[3]+x} ]] && err_exit '${x[0]+x} should be Empty'
326
unset x
327
[[ ${x[@]+x} ]] && err_exit '${x[@]+x} should be Empty'
328
unset x y z foo bar
329
330
{ x=$($SHELL -c '[[ (( $# -eq 0 )) ]] && print ok') 2> /dev/null;}
331
[[ $x == ok ]] || err_exit '((...)) inside [[...]] not treated as nested ()'
332
333
[[ -e /dev/fd/ ]] || err_exit '/dev/fd/ does not exits'
334
[[ -e /dev/tcp/ ]] || err_exit '/dev/tcp/ does not exist'
335
[[ -e /dev/udp/ ]] || err_exit '/dev/udp/ does not exist'
336
[[ -e /dev/xxx/ ]] && err_exit '/dev/xxx/ exists'
337
338
$SHELL 2> /dev/null -c '[[(-n foo)]]' || err_exit '[[(-n foo)]] should not require space in front of ('
339
340
$SHELL 2> /dev/null -c '[[ "]" == ~(E)[]] ]]' || err_exit 'pattern "~(E)[]]" does not match "]"'
341
342
unset var
343
[[ -v var ]] && err_exit '[[ -v var ]] should be false after unset var'
344
float var
345
[[ -v var ]] || err_exit '[[ -v var ]] should be true after float var'
346
unset var
347
[[ -v var ]] && err_exit '[[ -v var ]] should be false after unset var again'
348
349
test ! ! ! 2> /dev/null || err_exit 'test ! ! ! should return 0'
350
test ! ! x 2> /dev/null || err_exit 'test ! ! x should return 0'
351
test ! ! '' 2> /dev/null && err_exit 'test ! ! "" should return non-zero'
352
353
exit $((Errors<125?Errors:125))
354
355