xref: /titanic_51/usr/src/lib/libshell/common/tests/quoting2.sh (revision b0daa853ddd4c48b6374b8ac0dca46629b225c39)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2009 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#            http://www.opensource.org/licenses/cpl1.0.txt             #
11#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                  David Korn <dgk@research.att.com>                   #
18#                                                                      #
19########################################################################
20function err_exit
21{
22	print -u2 -n "\t"
23	print -u2 -r ${Command}[$1]: "${@:2}"
24	let Errors+=1
25}
26alias err_exit='err_exit $LINENO'
27
28Command=${0##*/}
29integer Errors=0
30set -o noglob
31if	[[ 'hi there' != "hi there" ]]
32then	err_exit "single quotes not the same as double quotes"
33fi
34x='hi there'
35if	[[ $x != 'hi there' ]]
36then	err_exit "$x not the same as 'hi there'"
37fi
38if	[[ $x != "hi there" ]]
39then	err_exit "$x not the same as \"hi there \""
40fi
41if	[[ \a\b\c\*\|\"\ \\ != 'abc*|" \' ]]
42then	err_exit " \\ differs from '' "
43fi
44if	[[ "ab\'\"\$(" != 'ab\'\''"$(' ]]
45then	err_exit " \"\" differs from '' "
46fi
47if	[[ $(print -r - 'abc*|" \') !=  'abc*|" \' ]]
48then	err_exit "\$(print -r - '') differs from ''"
49fi
50if	[[ $(print -r - "abc*|\" \\") !=  'abc*|" \' ]]
51then	err_exit "\$(print -r - '') differs from ''"
52fi
53if	[[ "$(print -r - 'abc*|" \')" !=  'abc*|" \' ]]
54then	err_exit "\"\$(print -r - '')\" differs from ''"
55fi
56if	[[ "$(print -r - "abc*|\" \\")" !=  'abc*|" \' ]]
57then	err_exit "\"\$(print -r - "")\" differs from ''"
58fi
59if	[[ $(print -r - "$(print -r - 'abc*|" \')") !=  'abc*|" \' ]]
60then	err_exit "nested \$(print -r - '') differs from ''"
61fi
62if	[[ "$(print -r - $(print -r - 'abc*|" \'))" !=  'abc*|" \' ]]
63then	err_exit "\"nested \$(print -r - '')\" differs from ''"
64fi
65if	[[ $(print -r - "$(print -r - 'abc*|" \')") !=  'abc*|" \' ]]
66then	err_exit "nested \"\$(print -r - '')\" differs from ''"
67fi
68unset x
69if	[[ ${x-$(print -r - "abc*|\" \\")} !=  'abc*|" \' ]]
70then	err_exit "\${x-\$(print -r - '')} differs from ''"
71fi
72if	[[ ${x-$(print -r - "a}c*|\" \\")} !=  'a}c*|" \' ]]
73then	err_exit "\${x-\$(print -r - '}')} differs from ''"
74fi
75x=$((echo foo)|(cat))
76if	[[ $x != foo  ]]
77then	err_exit "((cmd)|(cmd)) failed"
78fi
79x=$(print -r -- "\"$HOME\"")
80if	[[ $x != '"'$HOME'"' ]]
81then	err_exit "nested double quotes failed"
82fi
83: ${z="a{b}c"}
84if	[[ $z != 'a{b}c' ]]
85then	err_exit '${z="a{b}c"} not correct'
86fi
87unset z
88: "${z="a{b}c"}"
89if	[[ $z != 'a{b}c' ]]
90then	err_exit '"${z="a{b}c"}" not correct'
91fi
92if	[[ $(print -r -- "a\*b") !=  'a\*b' ]]
93then	err_exit '$(print -r -- "a\*b") differs from  a\*b'
94fi
95unset x
96if	[[ $(print -r -- "a\*b$x") !=  'a\*b' ]]
97then	err_exit '$(print -r -- "a\*b$x") differs from  a\*b'
98fi
99x=hello
100set -- ${x+foo bar bam}
101if	(( $# !=3 ))
102then	err_exit '${x+foo bar bam} does not yield three arguments'
103fi
104set -- ${x+foo "bar bam"}
105if	(( $# !=2 ))
106then	err_exit '${x+foo "bar bam"} does not yield two arguments'
107fi
108set -- ${x+foo 'bar bam'}
109if	(( $# !=2 ))
110then	err_exit '${x+foo '\''bar bam'\''} does not yield two arguments'
111fi
112set -- ${x+foo $x bam}
113if	(( $# !=3 ))
114then	err_exit '${x+foo $x bam} does not yield three arguments'
115fi
116set -- ${x+foo "$x" bam}
117if	(( $# !=3 ))
118then	err_exit '${x+foo "$x" bam} does not yield three arguments'
119fi
120set -- ${x+"foo $x bam"}
121if	(( $# !=1 ))
122then	err_exit '${x+"foo $x bam"} does not yield one argument'
123fi
124set -- "${x+foo $x bam}"
125if	(( $# !=1 ))
126then	err_exit '"${x+foo $x bam}" does not yield one argument'
127fi
128set -- ${x+foo "$x "bam}
129if	(( $# !=2 ))
130then	err_exit '${x+foo "$x "bam} does not yield two arguments'
131fi
132x="ab$'cd"
133if	[[ $x != 'ab$'"'cd" ]]
134then	err_exit '$'"' inside double quotes not working"
135fi
136x=`print 'ab$'`
137if	[[ $x != 'ab$' ]]
138then	err_exit '$'"' inside `` quotes not working"
139fi
140unset a
141x=$(print -r -- "'\
142\
143")
144if	[[ $x != "'" ]]
145then	err_exit 'line continuation in double strings not working'
146fi
147x=$(print -r -- "'\
148$a\
149")
150if	[[ $x != "'" ]]
151then	err_exit 'line continuation in expanded double strings not working'
152fi
153x='\*'
154if	[[ $(print -r -- $x) != '\*' ]]
155then	err_exit 'x="\\*";$x != \*'
156fi
157if	[[ $(print -r -- "\}" ) != '\}' ]]
158then	err_exit '(print -r -- "\}"' not working
159fi
160if	[[ $(print -r -- "\{" ) != '\{' ]]
161then	err_exit 'print -r -- "\{"' not working
162fi
163# The following caused a syntax error on earlier versions
164foo=foo x=-
165if	[[  `eval print \\${foo$x}` != foo* ]]
166then	err_exit '`eval  print \\${foo$x}`' not working
167fi
168if	[[  "`eval print \\${foo$x}`" != foo* ]]
169then	err_exit '"`eval  print \\${foo$x}`"' not working
170fi
171if	( [[ $() != '' ]] )
172then	err_exit '$() not working'
173fi
174x=a:b:c
175set -- $( IFS=:; print $x)
176if	(( $# != 3))
177then	err_exit 'IFS not working correctly with command substitution'
178fi
179$SHELL -n 2> /dev/null << \! || err_exit '$(...) bug with ( in comment'
180y=$(
181	# ( this line is a bug fix
182	print hi
183)
184!
185x=
186for j in  glob noglob
187do	for i in 'a\*b' 'a\ b' 'a\bc' 'a\*b' 'a\"b'
188	do	eval [[ '$('print -r -- \'$i\'\$x')' != "'$i'" ]]  && err_exit "quoting of $i\$x with $j enabled failed"
189		eval [[ '$('print -r -- \'$i\'\${x%*}')' != "'$i'" ]]  && err_exit "quoting of $i\${x%*} with $j enabled failed"
190		if	[[ $j == noglob ]]
191		then	eval [[ '$('print -r -- \'$i\'\${x:-*}')' != "'$i''*'" ]]  && err_exit "quoting of $i\${x:-*} with $j enabled failed"
192		fi
193	done
194	set -f
195done
196foo=foo
197[[ "$" == '$' ]] || err_exit '"$" != $'
198[[ "${foo}$" == 'foo$' ]] || err_exit 'foo=foo;"${foo}$" != foo$'
199[[ "${foo}${foo}$" == 'foofoo$' ]] || err_exit 'foo=foo;"${foo}${foo}$" != foofoo$'
200foo='$ '
201[[ "$foo" == ~(Elr)(\\\$|#)\  ]] || err_exit $'\'$ \' not matching RE \\\\\\$|#\''
202[[ "$foo" == ~(Elr)('\$'|#)\  ]] || err_exit $'\'$ \' not matching RE \'\\$\'|#\''
203foo='# '
204[[ "$foo" == ~(Elr)(\\\$|#)\  ]] || err_exit $'\'# \' not matching RE \\'\$|#\''
205[[ "$foo" == ~(Elr)('\$'|#)\  ]] || err_exit $'\'# \' not matching RE \'\\$\'|#\''
206[[ '\$' == '\$'* ]] ||   err_exit $'\'\\$\' not matching \'\\$\'*'
207[[ a+a == ~(E)a\+a ]] || err_exit '~(E)a\+a not matching a+a'
208[[ a+a =~ a\+a ]] || err_exit 'RE a\+a not matching a+a'
209exit $((Errors))
210