Print this page
XXX Remove nawk(1)
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libshell/common/tests/quoting.sh
+++ new/usr/src/lib/libshell/common/tests/quoting.sh
1 1 ########################################################################
2 2 # #
3 3 # This software is part of the ast package #
4 4 # Copyright (c) 1982-2010 AT&T Intellectual Property #
5 5 # and is licensed under the #
6 6 # Common Public License, Version 1.0 #
7 7 # by AT&T Intellectual Property #
8 8 # #
9 9 # A copy of the License is available at #
10 10 # http://www.opensource.org/licenses/cpl1.0.txt #
11 11 # (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) #
12 12 # #
13 13 # Information and Software Systems Research #
14 14 # AT&T Research #
15 15 # Florham Park NJ #
16 16 # #
17 17 # David Korn <dgk@research.att.com> #
18 18 # #
19 19 ########################################################################
20 20 function err_exit
21 21 {
22 22 print -u2 -n "\t"
23 23 print -u2 -r ${Command}[$1]: "${@:2}"
24 24 let Errors+=1
25 25 }
26 26 alias err_exit='err_exit $LINENO'
27 27
28 28 Command=${0##*/}
29 29 integer Errors=0
30 30 if [[ 'hi there' != "hi there" ]]
31 31 then err_exit "single quotes not the same as double quotes"
32 32 fi
33 33 x='hi there'
34 34 if [[ $x != 'hi there' ]]
35 35 then err_exit "$x not the same as 'hi there'"
36 36 fi
37 37 if [[ $x != "hi there" ]]
38 38 then err_exit "$x not the same as \"hi there \""
39 39 fi
40 40 if [[ \a\b\c\*\|\"\ \\ != 'abc*|" \' ]]
41 41 then err_exit " \\ differs from '' "
42 42 fi
43 43 if [[ "ab\'\"\$(" != 'ab\'\''"$(' ]]
44 44 then err_exit " \"\" differs from '' "
45 45 fi
46 46 if [[ $(print -r - 'abc*|" \') != 'abc*|" \' ]]
47 47 then err_exit "\$(print -r - '') differs from ''"
48 48 fi
49 49 if [[ $(print -r - "abc*|\" \\") != 'abc*|" \' ]]
50 50 then err_exit "\$(print -r - '') differs from ''"
51 51 fi
52 52 if [[ "$(print -r - 'abc*|" \')" != 'abc*|" \' ]]
53 53 then err_exit "\"\$(print -r - '')\" differs from ''"
54 54 fi
55 55 if [[ "$(print -r - "abc*|\" \\")" != 'abc*|" \' ]]
56 56 then err_exit "\"\$(print -r - "")\" differs from ''"
57 57 fi
58 58 if [[ $(print -r - $(print -r - 'abc*|" \')) != 'abc*|" \' ]]
59 59 then err_exit "nested \$(print -r - '') differs from ''"
60 60 fi
61 61 if [[ "$(print -r - $(print -r - 'abc*|" \'))" != 'abc*|" \' ]]
62 62 then err_exit "\"nested \$(print -r - '')\" differs from ''"
63 63 fi
64 64 if [[ $(print -r - "$(print -r - 'abc*|" \')") != 'abc*|" \' ]]
65 65 then err_exit "nested \"\$(print -r - '')\" differs from ''"
66 66 fi
67 67 unset x
68 68 if [[ ${x-$(print -r - "abc*|\" \\")} != 'abc*|" \' ]]
69 69 then err_exit "\${x-\$(print -r - '')} differs from ''"
70 70 fi
71 71 if [[ ${x-$(print -r - "a}c*|\" \\")} != 'a}c*|" \' ]]
72 72 then err_exit "\${x-\$(print -r - '}')} differs from ''"
73 73 fi
74 74 x=$((echo foo)|(cat))
75 75 if [[ $x != foo ]]
76 76 then err_exit "((cmd)|(cmd)) failed"
77 77 fi
78 78 x=$(print -r -- "\"$HOME\"")
79 79 if [[ $x != '"'$HOME'"' ]]
80 80 then err_exit "nested double quotes failed"
81 81 fi
82 82 : ${z="a{b}c"}
83 83 if [[ $z != 'a{b}c' ]]
84 84 then err_exit '${z="a{b}c"} not correct'
85 85 fi
86 86 unset z
87 87 : "${z="a{b}c"}"
88 88 if [[ $z != 'a{b}c' ]]
89 89 then err_exit '"${z="a{b}c"}" not correct'
90 90 fi
91 91 if [[ $(print -r -- "a\*b") != 'a\*b' ]]
92 92 then err_exit '$(print -r -- "a\*b") differs from a\*b'
93 93 fi
94 94 unset x
95 95 if [[ $(print -r -- "a\*b$x") != 'a\*b' ]]
96 96 then err_exit '$(print -r -- "a\*b$x") differs from a\*b'
97 97 fi
98 98 x=hello
99 99 set -- ${x+foo bar bam}
100 100 if (( $# !=3 ))
101 101 then err_exit '${x+foo bar bam} does not yield three arguments'
102 102 fi
103 103 set -- ${x+foo "bar bam"}
104 104 if (( $# !=2 ))
105 105 then err_exit '${x+foo "bar bam"} does not yield two arguments'
106 106 fi
107 107 set -- ${x+foo 'bar bam'}
108 108 if (( $# !=2 ))
109 109 then err_exit '${x+foo '\''bar bam'\''} does not yield two arguments'
110 110 fi
111 111 set -- ${x+foo $x bam}
112 112 if (( $# !=3 ))
113 113 then err_exit '${x+foo $x bam} does not yield three arguments'
114 114 fi
115 115 set -- ${x+foo "$x" bam}
116 116 if (( $# !=3 ))
117 117 then err_exit '${x+foo "$x" bam} does not yield three arguments'
118 118 fi
119 119 set -- ${x+"foo $x bam"}
120 120 if (( $# !=1 ))
121 121 then err_exit '${x+"foo $x bam"} does not yield one argument'
122 122 fi
123 123 set -- "${x+foo $x bam}"
124 124 if (( $# !=1 ))
125 125 then err_exit '"${x+foo $x bam}" does not yield one argument'
126 126 fi
127 127 set -- ${x+foo "$x "bam}
128 128 if (( $# !=2 ))
129 129 then err_exit '${x+foo "$x "bam} does not yield two arguments'
130 130 fi
131 131 x="ab$'cd"
132 132 if [[ $x != 'ab$'"'cd" ]]
133 133 then err_exit '$'"' inside double quotes not working"
134 134 fi
135 135 x=`print 'ab$'`
136 136 if [[ $x != 'ab$' ]]
137 137 then err_exit '$'"' inside `` quotes not working"
138 138 fi
139 139 unset a
140 140 x=$(print -r -- "'\
141 141 \
142 142 ")
143 143 if [[ $x != "'" ]]
144 144 then err_exit 'line continuation in double strings not working'
145 145 fi
146 146 x=$(print -r -- "'\
147 147 $a\
148 148 ")
149 149 if [[ $x != "'" ]]
150 150 then err_exit 'line continuation in expanded double strings not working'
151 151 fi
152 152 x='\*'
153 153 if [[ $(print -r -- $x) != '\*' ]]
154 154 then err_exit 'x="\\*";$x != \*'
155 155 fi
156 156 x=' hello world '
157 157 set -- $x
158 158 if (( $# != 2 ))
159 159 then err_exit 'field splitting error'
160 160 fi
161 161 x=$(print -r -- '1234567890123456789012345678901234567890123456789012345678901234567890 \
162 162 1234567890123456789012345678901234567890123456789012345678901234567890 \
163 163 1234567890123456789012345678901234567890123456789012345678901234567890 \
164 164 1234567890123456789012345678901234567890123456789012345678901234567890 \
165 165 1234567890123456789012345678901234567890123456789012345678901234567890 \
166 166 1234567890123456789012345678901234567890123456789012345678901234567890 \
167 167 1234567890123456789012345678901234567890123456789012345678901234567890 \
168 168 1234567890123456789012345678901234567890123456789012345678901234567890 \
169 169 1234567890123456789012345678901234567890123456789012345678901234567890 \
170 170 1234567890123456789012345678901234567890123456789012345678901234567890 \
171 171 1234567890123456789012345678901234567890123456789012345678901234567890 \
172 172 1234567890123456789012345678901234567890123456789012345678901234567890 \
173 173 1234567890123456789012345678901234567890123456789012345678901234567890 \
174 174 1234567890123456789012345678901234567890123456789012345678901234567890 \
175 175 1234567890123456789012345678901234567890123456789012345678901234567890')
176 176 if (( ${#x} != (15*73-3) ))
177 177 then err_exit "length of x, ${#x}, is incorrect should be $((15*73-3))"
178 178 fi
179 179 x='$hi'
180 180 if [[ $x\$ != '$hi$' ]]
181 181 then err_exit ' $x\$, with x=$hi, does not expand to $hi$'
182 182 fi
183 183 if [[ $x$ != '$hi$' ]]
184 184 then err_exit ' $x$, with x=$hi, does not expand to $hi$'
185 185 fi
186 186 set -- $(/bin/echo foo;sleep 1;/bin/echo bar)
187 187 if [[ $# != 2 ]]
188 188 then err_exit 'word splitting after command substitution not working'
189 189 fi
190 190 unset q
191 191 if [[ "${q:+'}q${q:+'}" != q ]]
192 192 then err_exit 'expansion of "{q:+'\''}" not correct when q unset'
193 193 fi
194 194 q=1
195 195 if [[ "${q:+'}q${q:+'}" != "'q'" ]]
196 196 then err_exit 'expansion of "{q:+'\''}" not correct when q set'
197 197 fi
198 198 x=$'x\' #y'
199 199 if [[ $x != "x' #y" ]]
200 200 then err_exit "$'x\' #y'" not working
201 201 fi
202 202 x=$q$'x\' #y'
203 203 if [[ $x != "1x' #y" ]]
204 204 then err_exit "$q$'x\' #y'" not working
205 205 fi
206 206 IFS=,
207 207 x='a,b\,c,d'
208 208 set -- $x
209 209 if [[ $2 != 'b\' ]]
210 210 then err_exit "field splitting of $x with IFS=$IFS not working"
211 211 fi
212 212 foo=bar
213 213 bar=$(print -r -- ${foo+\\n\ })
214 214 if [[ $bar != '\n ' ]]
215 215 then err_exit '${foo+\\n\ } expansion error'
216 216 fi
217 217 unset bar
218 218 bar=$(print -r -- ${foo+\\n\ $bar})
219 219 if [[ $bar != '\n ' ]]
220 220 then err_exit '${foo+\\n\ $bar} expansion error with bar unset'
221 221 fi
222 222 x='\\(..\\)|&\|\|\\&\\|'
223 223 if [[ $(print -r -- $x) != "$x" ]]
224 224 then err_exit '$x, where x=\\(..\\)|&\|\|\\&\\| not working'
225 225 fi
226 226 x='\\('
227 227 if [[ $(print -r -- a${x}b) != a"${x}"b ]]
228 228 then err_exit 'a${x}b, where x=\\( not working'
229 229 fi
230 230 x=
231 231 if [[ $(print -r -- $x'\\1') != '\\1' ]]
232 232 then err_exit 'backreference inside single quotes broken'
233 233 fi
234 234 set -- ''
235 235 set -- "$@"
236 236 if (( $# != 1 ))
237 237 then err_exit '"$@" not preserving nulls'
238 238 fi
239 239 x=
240 240 if [[ $(print -r s"!\2${x}\1\a!") != 's!\2\1\a!' ]]
241 241 then err_exit 'print -r s"!\2${x}\1\a!" not equal s!\2\1\a!'
242 242 fi
243 243 if [[ $(print -r $'foo\n\n\n') != foo ]]
244 244 then err_exit 'trailing newlines on comsubstitution not removed'
245 245 fi
246 246 unset x
247 247 if [[ ${x:='//'} != '//' ]]
248 248 then err_exit '${x:='//'} != "//"'
249 249 fi
250 250 if [[ $(print -r "\"hi$\"") != '"hi$"' ]]
251 251 then err_exit '$\ not correct inside ""'
252 252 fi
253 253 unset x
254 254 if [[ "${x-a\}b}" != 'a}b' ]]
255 255 then err_exit '"${x-a\}b}" != "a}b"'
256 256 fi
257 257 if [[ "\}\]$x\*\{\[\\" != '\}\]\*\{\[\' ]]
258 258 then err_exit '"\}\]$x\*\{\[\\" != "\}\]\*\{\[\"'
259 259 fi
260 260 foo=yes
261 261 if [[ $(print -r -- {\$foo}) != '{$foo}' ]]
262 262 then err_exit '{\$foo}' not expanded correctly
263 263 fi
264 264 [[ foo == $(
265 265 ###########################################################
266 266 ###########################################################
267 267 ###########################################################
268 268 ###########################################################
269 269 ###########################################################
270 270 ###########################################################
271 271 ###########################################################
272 272 ###########################################################
273 273 ###########################################################
274 274 ###########################################################
275 275 ###########################################################
276 276 ###########################################################
277 277 ###########################################################
278 278 ###########################################################
279 279 ###########################################################
280 280 ###########################################################
281 281 ###########################################################
282 282 ###########################################################
283 283 ###########################################################
284 284 ###########################################################
285 285 ###########################################################
286 286 ###########################################################
287 287 ###########################################################
288 288 ###########################################################
289 289 ###########################################################
290 290 ###########################################################
291 291 ###########################################################
292 292 ###########################################################
293 293 ###########################################################
294 294 ###########################################################
295 295 ###########################################################
296 296 ###########################################################
297 297 ###########################################################
298 298 ###########################################################
299 299 ###########################################################
300 300 ###########################################################
301 301 ###########################################################
302 302 ###########################################################
303 303 print foo) ]] || err_exit "command subsitution with long comments broken"
304 304 subject='some/other/words'
305 305 re='(?*)/(?*)/(?*)'
306 306 [[ ${subject/${re}/\3} != words ]] && err_exit 'string replacement with \3 not working'
307 307 [[ ${subject/${re}/'\3'} != '\3' ]] && err_exit 'string replacement with '"'\3'"' not working'
308 308 [[ ${subject/${re}/"\\3"} != '\3' ]] && err_exit 'string replacement with "\\3" not working'
309 309 [[ ${subject/${re}/"\3"} != '\3' ]] && err_exit 'string replacement with "\3" not working'
310 310 string='\3'
311 311 [[ ${subject/${re}/${string}} != words ]] && err_exit 'string replacement with $string not working with string=\3'
312 312 [[ $(print -r "${subject/${re}/${string}}") != words ]] && err_exit 'string replacement with $string not working with string=\3 using print'
313 313 [[ ${subject/${re}/"${string}"} != '\3' ]] && err_exit 'string replacement with "$string" not working with string=\3'
314 314 [[ $(print -r "${subject/${re}/"${string}"}") != '\3' ]] && err_exit 'string replacement with "$string" not working with string=\3 using print'
315 315 string='\\3'
316 316 [[ ${subject/${re}/${string}} != '\3' ]] && err_exit 'string replacement with $string not working with string=\\3'
317 317 [[ ${subject/${re}/"${string}"} != '\\3' ]] && err_exit 'string replacement with "$string" not working with string=\\3'
318 318 [[ ${subject/${re}/\4} != '\4' ]] && err_exit 'string replacement with \4 not working'
319 319 [[ ${subject/${re}/'\4'} != '\4' ]] && err_exit 'string replacement with '\4' not working'
320 320 string='\4'
321 321 [[ ${subject/${re}/${string}} != '\4' ]] && err_exit 'string replacement with $string not working with string=\4'
↓ open down ↓ |
321 lines elided |
↑ open up ↑ |
322 322 [[ ${subject/${re}/"${string}"} != '\4' ]] && err_exit 'string replacement with "$string" not working with string=\4'
323 323 string='&foo'
324 324 [[ ${subject/${re}/${string}} != '&foo' ]] && err_exit 'string replacement with $string not working with string=&foo'
325 325 [[ ${subject/${re}/"${string}"} != '&foo' ]] && err_exit 'string replacement with "$string" not working with string=&foo'
326 326 {
327 327 x=x
328 328 x=${x:-`id | sed 's/^[^(]*(\([^)]*\)).*/\1/'`}
329 329 } 2> /dev/null || err_exit 'skipping over `` failed'
330 330 [[ $x == x ]] || err_exit 'assignment ${x:=`...`} failed'
331 331 [[ $($SHELL -c 'print a[') == 'a[' ]] || err_exit "unbalanced '[' in command arg fails"
332 -$SHELL -c $'false && (( `wc -l /dev/null | nawk \'{print $1}\'` > 2 )) && true;:' 2> /dev/null || err_exit 'syntax error with ` in arithmetic expression'
332 +$SHELL -c $'false && (( `wc -l /dev/null | /usr/xpg4/bin/awk \'{print $1}\'` > 2 )) && true;:' 2> /dev/null || err_exit 'syntax error with ` in arithmetic expression'
333 333 { $SHELL -c '(( 1`: "{ }"` ))' ;} 2> /dev/null || err_exit 'problem with ` inside (())'
334 334 varname=foobarx
335 335 x=`print '"\$'${varname}'"'`
336 336 [[ $x == '"$foobarx"' ]] || err_exit $'\\$\' not handled correctly inside ``'
337 337
338 338 copy1=5 copynum=1
339 339 foo="`eval echo "$"{copy$copynum"-0}"`"
340 340 [[ $foo == "$copy1" ]] || err_exit '$"..." not being ignored inside ``'
341 341
342 342 exit $((Errors))
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX