Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/ksh93/tests/quoting.sh
1810 views
1
########################################################################
2
# #
3
# This software is part of the ast package #
4
# Copyright (c) 1982-2011 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
(( Errors++ ))
25
}
26
alias err_exit='err_exit $LINENO'
27
28
Command=${0##*/}
29
integer Errors=0
30
if [[ 'hi there' != "hi there" ]]
31
then err_exit "single quotes not the same as double quotes"
32
fi
33
x='hi there'
34
if [[ $x != 'hi there' ]]
35
then err_exit "$x not the same as 'hi there'"
36
fi
37
if [[ $x != "hi there" ]]
38
then err_exit "$x not the same as \"hi there \""
39
fi
40
if [[ \a\b\c\*\|\"\ \\ != 'abc*|" \' ]]
41
then err_exit " \\ differs from '' "
42
fi
43
if [[ "ab\'\"\$(" != 'ab\'\''"$(' ]]
44
then err_exit " \"\" differs from '' "
45
fi
46
if [[ $(print -r - 'abc*|" \') != 'abc*|" \' ]]
47
then err_exit "\$(print -r - '') differs from ''"
48
fi
49
if [[ $(print -r - "abc*|\" \\") != 'abc*|" \' ]]
50
then err_exit "\$(print -r - '') differs from ''"
51
fi
52
if [[ "$(print -r - 'abc*|" \')" != 'abc*|" \' ]]
53
then err_exit "\"\$(print -r - '')\" differs from ''"
54
fi
55
if [[ "$(print -r - "abc*|\" \\")" != 'abc*|" \' ]]
56
then err_exit "\"\$(print -r - "")\" differs from ''"
57
fi
58
if [[ $(print -r - $(print -r - 'abc*|" \')) != 'abc*|" \' ]]
59
then err_exit "nested \$(print -r - '') differs from ''"
60
fi
61
if [[ "$(print -r - $(print -r - 'abc*|" \'))" != 'abc*|" \' ]]
62
then err_exit "\"nested \$(print -r - '')\" differs from ''"
63
fi
64
if [[ $(print -r - "$(print -r - 'abc*|" \')") != 'abc*|" \' ]]
65
then err_exit "nested \"\$(print -r - '')\" differs from ''"
66
fi
67
unset x
68
if [[ ${x-$(print -r - "abc*|\" \\")} != 'abc*|" \' ]]
69
then err_exit "\${x-\$(print -r - '')} differs from ''"
70
fi
71
if [[ ${x-$(print -r - "a}c*|\" \\")} != 'a}c*|" \' ]]
72
then err_exit "\${x-\$(print -r - '}')} differs from ''"
73
fi
74
x=$((echo foo)|(cat))
75
if [[ $x != foo ]]
76
then err_exit "((cmd)|(cmd)) failed"
77
fi
78
x=$(print -r -- "\"$HOME\"")
79
if [[ $x != '"'$HOME'"' ]]
80
then err_exit "nested double quotes failed"
81
fi
82
unset z
83
: ${z="a{b}c"}
84
if [[ $z != 'a{b}c' ]]
85
then err_exit '${z="a{b}c"} not correct'
86
fi
87
unset z
88
: "${z="a{b}c"}"
89
if [[ $z != 'a{b}c' ]]
90
then err_exit '"${z="a{b}c"}" not correct'
91
fi
92
if [[ $(print -r -- "a\*b") != 'a\*b' ]]
93
then err_exit '$(print -r -- "a\*b") differs from a\*b'
94
fi
95
unset x
96
if [[ $(print -r -- "a\*b$x") != 'a\*b' ]]
97
then err_exit '$(print -r -- "a\*b$x") differs from a\*b'
98
fi
99
x=hello
100
set -- ${x+foo bar bam}
101
if (( $# !=3 ))
102
then err_exit '${x+foo bar bam} does not yield three arguments'
103
fi
104
set -- ${x+foo "bar bam"}
105
if (( $# !=2 ))
106
then err_exit '${x+foo "bar bam"} does not yield two arguments'
107
fi
108
set -- ${x+foo 'bar bam'}
109
if (( $# !=2 ))
110
then err_exit '${x+foo '\''bar bam'\''} does not yield two arguments'
111
fi
112
set -- ${x+foo $x bam}
113
if (( $# !=3 ))
114
then err_exit '${x+foo $x bam} does not yield three arguments'
115
fi
116
set -- ${x+foo "$x" bam}
117
if (( $# !=3 ))
118
then err_exit '${x+foo "$x" bam} does not yield three arguments'
119
fi
120
set -- ${x+"foo $x bam"}
121
if (( $# !=1 ))
122
then err_exit '${x+"foo $x bam"} does not yield one argument'
123
fi
124
set -- "${x+foo $x bam}"
125
if (( $# !=1 ))
126
then err_exit '"${x+foo $x bam}" does not yield one argument'
127
fi
128
set -- ${x+foo "$x "bam}
129
if (( $# !=2 ))
130
then err_exit '${x+foo "$x "bam} does not yield two arguments'
131
fi
132
x="ab$'cd"
133
if [[ $x != 'ab$'"'cd" ]]
134
then err_exit '$'"' inside double quotes not working"
135
fi
136
x=`print 'ab$'`
137
if [[ $x != 'ab$' ]]
138
then err_exit '$'"' inside `` quotes not working"
139
fi
140
unset a
141
x=$(print -r -- "'\
142
\
143
")
144
if [[ $x != "'" ]]
145
then err_exit 'line continuation in double strings not working'
146
fi
147
x=$(print -r -- "'\
148
$a\
149
")
150
if [[ $x != "'" ]]
151
then err_exit 'line continuation in expanded double strings not working'
152
fi
153
x='\*'
154
if [[ $(print -r -- $x) != '\*' ]]
155
then err_exit 'x="\\*";$x != \*'
156
fi
157
x=' hello world '
158
set -- $x
159
if (( $# != 2 ))
160
then err_exit 'field splitting error'
161
fi
162
x=$(print -r -- '1234567890123456789012345678901234567890123456789012345678901234567890 \
163
1234567890123456789012345678901234567890123456789012345678901234567890 \
164
1234567890123456789012345678901234567890123456789012345678901234567890 \
165
1234567890123456789012345678901234567890123456789012345678901234567890 \
166
1234567890123456789012345678901234567890123456789012345678901234567890 \
167
1234567890123456789012345678901234567890123456789012345678901234567890 \
168
1234567890123456789012345678901234567890123456789012345678901234567890 \
169
1234567890123456789012345678901234567890123456789012345678901234567890 \
170
1234567890123456789012345678901234567890123456789012345678901234567890 \
171
1234567890123456789012345678901234567890123456789012345678901234567890 \
172
1234567890123456789012345678901234567890123456789012345678901234567890 \
173
1234567890123456789012345678901234567890123456789012345678901234567890 \
174
1234567890123456789012345678901234567890123456789012345678901234567890 \
175
1234567890123456789012345678901234567890123456789012345678901234567890 \
176
1234567890123456789012345678901234567890123456789012345678901234567890')
177
if (( ${#x} != (15*73-3) ))
178
then err_exit "length of x, ${#x}, is incorrect should be $((15*73-3))"
179
fi
180
x='$hi'
181
if [[ $x\$ != '$hi$' ]]
182
then err_exit ' $x\$, with x=$hi, does not expand to $hi$'
183
fi
184
if [[ $x$ != '$hi$' ]]
185
then err_exit ' $x$, with x=$hi, does not expand to $hi$'
186
fi
187
set -- $(/bin/echo foo;sleep 1;/bin/echo bar)
188
if [[ $# != 2 ]]
189
then err_exit 'word splitting after command substitution not working'
190
fi
191
unset q
192
if [[ "${q:+'}q${q:+'}" != q ]]
193
then err_exit 'expansion of "{q:+'\''}" not correct when q unset'
194
fi
195
q=1
196
if [[ "${q:+'}q${q:+'}" != "'q'" ]]
197
then err_exit 'expansion of "{q:+'\''}" not correct when q set'
198
fi
199
x=$'x\' #y'
200
if [[ $x != "x' #y" ]]
201
then err_exit "$'x\' #y'" not working
202
fi
203
x=$q$'x\' #y'
204
if [[ $x != "1x' #y" ]]
205
then err_exit "$q$'x\' #y'" not working
206
fi
207
IFS=,
208
x='a,b\,c,d'
209
set -- $x
210
if [[ $2 != 'b\' ]]
211
then err_exit "field splitting of $x with IFS=$IFS not working"
212
fi
213
foo=bar
214
bar=$(print -r -- ${foo+\\n\ })
215
if [[ $bar != '\n ' ]]
216
then err_exit '${foo+\\n\ } expansion error'
217
fi
218
unset bar
219
bar=$(print -r -- ${foo+\\n\ $bar})
220
if [[ $bar != '\n ' ]]
221
then err_exit '${foo+\\n\ $bar} expansion error with bar unset'
222
fi
223
x='\\(..\\)|&\|\|\\&\\|'
224
if [[ $(print -r -- $x) != "$x" ]]
225
then err_exit '$x, where x=\\(..\\)|&\|\|\\&\\| not working'
226
fi
227
x='\\('
228
if [[ $(print -r -- a${x}b) != a"${x}"b ]]
229
then err_exit 'a${x}b, where x=\\( not working'
230
fi
231
x=
232
if [[ $(print -r -- $x'\\1') != '\\1' ]]
233
then err_exit 'backreference inside single quotes broken'
234
fi
235
set -- ''
236
set -- "$@"
237
if (( $# != 1 ))
238
then err_exit '"$@" not preserving nulls'
239
fi
240
x=
241
if [[ $(print -r s"!\2${x}\1\a!") != 's!\2\1\a!' ]]
242
then err_exit 'print -r s"!\2${x}\1\a!" not equal s!\2\1\a!'
243
fi
244
if [[ $(print -r $'foo\n\n\n') != foo ]]
245
then err_exit 'trailing newlines on comsubstitution not removed'
246
fi
247
unset x
248
if [[ ${x:='//'} != '//' ]]
249
then err_exit '${x:='//'} != "//"'
250
fi
251
if [[ $(print -r "\"hi$\"") != '"hi$"' ]]
252
then err_exit '$\ not correct inside ""'
253
fi
254
unset x
255
if [[ "${x-a\}b}" != 'a}b' ]]
256
then err_exit '"${x-a\}b}" != "a}b"'
257
fi
258
if [[ "\}\]$x\*\{\[\\" != '\}\]\*\{\[\' ]]
259
then err_exit '"\}\]$x\*\{\[\\" != "\}\]\*\{\[\"'
260
fi
261
foo=yes
262
if [[ $(print -r -- {\$foo}) != '{$foo}' ]]
263
then err_exit '{\$foo}' not expanded correctly
264
fi
265
[[ foo == $(
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
print foo) ]] || err_exit "command subsitution with long comments broken"
305
subject='some/other/words'
306
re='@(?*)/@(?*)/@(?*)'
307
[[ ${subject/${re}/\3} != words ]] && err_exit 'string replacement with \3 not working'
308
[[ ${subject/${re}/'\3'} != '\3' ]] && err_exit 'string replacement with '"'\3'"' not working'
309
[[ ${subject/${re}/"\\3"} != '\3' ]] && err_exit 'string replacement with "\\3" not working'
310
[[ ${subject/${re}/"\3"} != '\3' ]] && err_exit 'string replacement with "\3" not working'
311
string='\3'
312
[[ ${subject/${re}/${string}} != words ]] && err_exit 'string replacement with $string not working with string=\3'
313
[[ $(print -r "${subject/${re}/${string}}") != words ]] && err_exit 'string replacement with $string not working with string=\3 using print'
314
[[ ${subject/${re}/"${string}"} != '\3' ]] && err_exit 'string replacement with "$string" not working with string=\3'
315
[[ $(print -r "${subject/${re}/"${string}"}") != '\3' ]] && err_exit 'string replacement with "$string" not working with string=\3 using print'
316
string='\\3'
317
[[ ${subject/${re}/${string}} != '\3' ]] && err_exit 'string replacement with $string not working with string=\\3'
318
[[ ${subject/${re}/"${string}"} != '\\3' ]] && err_exit 'string replacement with "$string" not working with string=\\3'
319
[[ ${subject/${re}/\4} != '\4' ]] && err_exit 'string replacement with \4 not working'
320
[[ ${subject/${re}/'\4'} != '\4' ]] && err_exit 'string replacement with '\4' not working'
321
string='\4'
322
[[ ${subject/${re}/${string}} != '\4' ]] && err_exit 'string replacement with $string not working with string=\4'
323
[[ ${subject/${re}/"${string}"} != '\4' ]] && err_exit 'string replacement with "$string" not working with string=\4'
324
string='&foo'
325
[[ ${subject/${re}/${string}} != '&foo' ]] && err_exit 'string replacement with $string not working with string=&foo'
326
[[ ${subject/${re}/"${string}"} != '&foo' ]] && err_exit 'string replacement with "$string" not working with string=&foo'
327
{
328
x=x
329
x=${x:-`id | sed 's/^[^(]*(\([^)]*\)).*/\1/'`}
330
} 2> /dev/null || err_exit 'skipping over `` failed'
331
[[ $x == x ]] || err_exit 'assignment ${x:=`...`} failed'
332
[[ $($SHELL -c 'print a[') == 'a[' ]] || err_exit "unbalanced '[' in command arg fails"
333
$SHELL -c $'false && (( `wc -l /dev/null | nawk \'{print $1}\'` > 2 )) && true;:' 2> /dev/null || err_exit 'syntax error with ` in arithmetic expression'
334
{ $SHELL -c '(( 1`: "{ }"` ))' ;} 2> /dev/null || err_exit 'problem with ` inside (())'
335
varname=foobarx
336
x=`print '"\$'${varname}'"'`
337
[[ $x == '"$foobarx"' ]] || err_exit $'\\$\' not handled correctly inside ``'
338
339
copy1=5 copynum=1
340
foo="`eval echo "$"{copy$copynum"-0}"`"
341
[[ $foo == "$copy1" ]] || err_exit '$"..." not being ignored inside ``'
342
343
[[ $($SHELL -c 'set -- ${1+"$@"}; print $#' cmd '') == 1 ]] || err_exit '${1+"$@"} with one empty argument fails'
344
[[ $($SHELL -c 'set -- ${1+"$@"}; print $#' cmd foo '') == 2 ]] || err_exit '${1+"$@"} with one non-empty and on empty argument fails'
345
[[ $($SHELL -c 'set -- ${1+"$@"}; print $#' cmd "" '') == 2 ]] || err_exit '${1+"$@"} with two empty arguments fails'
346
[[ $($SHELL -c 'set -- ${1+"$@"}; print $#' cmd "" '' '') == 3 ]] || err_exit '${1+"$@"} with three empty arguments fails'
347
[[ $($SHELL -c 'set -- "$@"; print $#' cmd '') == 1 ]] || err_exit '"$@" with one empty argument fails'
348
[[ $($SHELL -c 'set -- "${@:2}"; print $#' cmd '') == 0 ]] || err_exit '"$@" with one empty argument fails'
349
[[ $($SHELL -c 'set -- "$@"; print $#' cmd foo '') == 2 ]] || err_exit '"$@" with one non-empty and on empty argument fails'
350
[[ $($SHELL -c 'set -- "$@"; print $#' cmd "" '') == 2 ]] || err_exit '"$@" with two empty arguments fails'
351
[[ $($SHELL -c 'set -- "$@"; print $#' cmd "" '' '') == 3 ]] || err_exit '"$@" with three empty arguments fails'
352
args=('')
353
set -- "${args[@]}"
354
[[ $# == 1 ]] || err_exit '"${args[@]}"} with one empty argument fails'
355
set -- ${1+"${args[@]}"}
356
[[ $# == 1 ]] || err_exit '${1+"${args[@]}"} with one empty argument fails'
357
args=(foo '')
358
set -- "${args[@]}"
359
[[ $# == 2 ]] || err_exit '"${args[@]}"} with one non-empty and one empty argument fails'
360
set -- ${1+"${args[@]}"}
361
[[ $# == 2 ]] || err_exit '${1+"${args[@]}"} with one non-empty and one empty argument fails'
362
363
unset ARGS
364
set --
365
ARGS=("$@")
366
set -- "${ARGS[@]}"
367
(( $# )) && err_exit 'set -- "${ARGS[@]}" for empty array should not produce arguments'
368
369
exit $((Errors<125?Errors:125))
370
371