xref: /onnv-gate/usr/src/lib/libshell/common/tests/quoting2.sh (revision 12068:08a39a083754)
14887Schin########################################################################
24887Schin#                                                                      #
34887Schin#               This software is part of the ast package               #
4*12068SRoger.Faulkner@Oracle.COM#          Copyright (c) 1982-2010 AT&T Intellectual Property          #
54887Schin#                      and is licensed under the                       #
64887Schin#                  Common Public License, Version 1.0                  #
78462SApril.Chin@Sun.COM#                    by AT&T Intellectual Property                     #
84887Schin#                                                                      #
94887Schin#                A copy of the License is available at                 #
104887Schin#            http://www.opensource.org/licenses/cpl1.0.txt             #
114887Schin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
124887Schin#                                                                      #
134887Schin#              Information and Software Systems Research               #
144887Schin#                            AT&T Research                             #
154887Schin#                           Florham Park NJ                            #
164887Schin#                                                                      #
174887Schin#                  David Korn <dgk@research.att.com>                   #
184887Schin#                                                                      #
194887Schin########################################################################
204887Schinfunction err_exit
214887Schin{
224887Schin	print -u2 -n "\t"
234887Schin	print -u2 -r ${Command}[$1]: "${@:2}"
244887Schin	let Errors+=1
254887Schin}
264887Schinalias err_exit='err_exit $LINENO'
274887Schin
284887SchinCommand=${0##*/}
294887Schininteger Errors=0
304887Schinset -o noglob
314887Schinif	[[ 'hi there' != "hi there" ]]
324887Schinthen	err_exit "single quotes not the same as double quotes"
334887Schinfi
344887Schinx='hi there'
354887Schinif	[[ $x != 'hi there' ]]
364887Schinthen	err_exit "$x not the same as 'hi there'"
374887Schinfi
384887Schinif	[[ $x != "hi there" ]]
394887Schinthen	err_exit "$x not the same as \"hi there \""
404887Schinfi
414887Schinif	[[ \a\b\c\*\|\"\ \\ != 'abc*|" \' ]]
424887Schinthen	err_exit " \\ differs from '' "
434887Schinfi
444887Schinif	[[ "ab\'\"\$(" != 'ab\'\''"$(' ]]
454887Schinthen	err_exit " \"\" differs from '' "
464887Schinfi
474887Schinif	[[ $(print -r - 'abc*|" \') !=  'abc*|" \' ]]
484887Schinthen	err_exit "\$(print -r - '') differs from ''"
494887Schinfi
504887Schinif	[[ $(print -r - "abc*|\" \\") !=  'abc*|" \' ]]
514887Schinthen	err_exit "\$(print -r - '') differs from ''"
524887Schinfi
534887Schinif	[[ "$(print -r - 'abc*|" \')" !=  'abc*|" \' ]]
544887Schinthen	err_exit "\"\$(print -r - '')\" differs from ''"
554887Schinfi
564887Schinif	[[ "$(print -r - "abc*|\" \\")" !=  'abc*|" \' ]]
574887Schinthen	err_exit "\"\$(print -r - "")\" differs from ''"
584887Schinfi
594887Schinif	[[ $(print -r - "$(print -r - 'abc*|" \')") !=  'abc*|" \' ]]
604887Schinthen	err_exit "nested \$(print -r - '') differs from ''"
614887Schinfi
624887Schinif	[[ "$(print -r - $(print -r - 'abc*|" \'))" !=  'abc*|" \' ]]
634887Schinthen	err_exit "\"nested \$(print -r - '')\" differs from ''"
644887Schinfi
654887Schinif	[[ $(print -r - "$(print -r - 'abc*|" \')") !=  'abc*|" \' ]]
664887Schinthen	err_exit "nested \"\$(print -r - '')\" differs from ''"
674887Schinfi
684887Schinunset x
694887Schinif	[[ ${x-$(print -r - "abc*|\" \\")} !=  'abc*|" \' ]]
704887Schinthen	err_exit "\${x-\$(print -r - '')} differs from ''"
714887Schinfi
724887Schinif	[[ ${x-$(print -r - "a}c*|\" \\")} !=  'a}c*|" \' ]]
734887Schinthen	err_exit "\${x-\$(print -r - '}')} differs from ''"
744887Schinfi
754887Schinx=$((echo foo)|(cat))
764887Schinif	[[ $x != foo  ]]
774887Schinthen	err_exit "((cmd)|(cmd)) failed"
784887Schinfi
794887Schinx=$(print -r -- "\"$HOME\"")
804887Schinif	[[ $x != '"'$HOME'"' ]]
814887Schinthen	err_exit "nested double quotes failed"
824887Schinfi
834887Schin: ${z="a{b}c"}
844887Schinif	[[ $z != 'a{b}c' ]]
854887Schinthen	err_exit '${z="a{b}c"} not correct'
864887Schinfi
874887Schinunset z
884887Schin: "${z="a{b}c"}"
894887Schinif	[[ $z != 'a{b}c' ]]
904887Schinthen	err_exit '"${z="a{b}c"}" not correct'
914887Schinfi
924887Schinif	[[ $(print -r -- "a\*b") !=  'a\*b' ]]
934887Schinthen	err_exit '$(print -r -- "a\*b") differs from  a\*b'
944887Schinfi
954887Schinunset x
964887Schinif	[[ $(print -r -- "a\*b$x") !=  'a\*b' ]]
974887Schinthen	err_exit '$(print -r -- "a\*b$x") differs from  a\*b'
984887Schinfi
994887Schinx=hello
1004887Schinset -- ${x+foo bar bam}
1014887Schinif	(( $# !=3 ))
1024887Schinthen	err_exit '${x+foo bar bam} does not yield three arguments'
1034887Schinfi
1044887Schinset -- ${x+foo "bar bam"}
1054887Schinif	(( $# !=2 ))
1064887Schinthen	err_exit '${x+foo "bar bam"} does not yield two arguments'
1074887Schinfi
1084887Schinset -- ${x+foo 'bar bam'}
1094887Schinif	(( $# !=2 ))
1104887Schinthen	err_exit '${x+foo '\''bar bam'\''} does not yield two arguments'
1114887Schinfi
1124887Schinset -- ${x+foo $x bam}
1134887Schinif	(( $# !=3 ))
1144887Schinthen	err_exit '${x+foo $x bam} does not yield three arguments'
1154887Schinfi
1164887Schinset -- ${x+foo "$x" bam}
1174887Schinif	(( $# !=3 ))
1184887Schinthen	err_exit '${x+foo "$x" bam} does not yield three arguments'
1194887Schinfi
1204887Schinset -- ${x+"foo $x bam"}
1214887Schinif	(( $# !=1 ))
1224887Schinthen	err_exit '${x+"foo $x bam"} does not yield one argument'
1234887Schinfi
1244887Schinset -- "${x+foo $x bam}"
1254887Schinif	(( $# !=1 ))
1264887Schinthen	err_exit '"${x+foo $x bam}" does not yield one argument'
1274887Schinfi
1284887Schinset -- ${x+foo "$x "bam}
1294887Schinif	(( $# !=2 ))
1304887Schinthen	err_exit '${x+foo "$x "bam} does not yield two arguments'
1314887Schinfi
1324887Schinx="ab$'cd"
1334887Schinif	[[ $x != 'ab$'"'cd" ]]
1344887Schinthen	err_exit '$'"' inside double quotes not working"
1354887Schinfi
1364887Schinx=`print 'ab$'`
1374887Schinif	[[ $x != 'ab$' ]]
1384887Schinthen	err_exit '$'"' inside `` quotes not working"
1394887Schinfi
1404887Schinunset a
1414887Schinx=$(print -r -- "'\
1424887Schin\
1434887Schin")
1444887Schinif	[[ $x != "'" ]]
1454887Schinthen	err_exit 'line continuation in double strings not working'
1464887Schinfi
1474887Schinx=$(print -r -- "'\
1484887Schin$a\
1494887Schin")
1504887Schinif	[[ $x != "'" ]]
1514887Schinthen	err_exit 'line continuation in expanded double strings not working'
1524887Schinfi
1534887Schinx='\*'
1544887Schinif	[[ $(print -r -- $x) != '\*' ]]
1554887Schinthen	err_exit 'x="\\*";$x != \*'
1564887Schinfi
1574887Schinif	[[ $(print -r -- "\}" ) != '\}' ]]
1584887Schinthen	err_exit '(print -r -- "\}"' not working
1594887Schinfi
1604887Schinif	[[ $(print -r -- "\{" ) != '\{' ]]
1614887Schinthen	err_exit 'print -r -- "\{"' not working
1624887Schinfi
1634887Schin# The following caused a syntax error on earlier versions
1644887Schinfoo=foo x=-
1654887Schinif	[[  `eval print \\${foo$x}` != foo* ]]
1664887Schinthen	err_exit '`eval  print \\${foo$x}`' not working
1674887Schinfi
1684887Schinif	[[  "`eval print \\${foo$x}`" != foo* ]]
1694887Schinthen	err_exit '"`eval  print \\${foo$x}`"' not working
1704887Schinfi
1714887Schinif	( [[ $() != '' ]] )
1724887Schinthen	err_exit '$() not working'
1734887Schinfi
1744887Schinx=a:b:c
1754887Schinset -- $( IFS=:; print $x)
1764887Schinif	(( $# != 3))
1774887Schinthen	err_exit 'IFS not working correctly with command substitution'
1784887Schinfi
1794887Schin$SHELL -n 2> /dev/null << \! || err_exit '$(...) bug with ( in comment'
1804887Schiny=$(
1814887Schin	# ( this line is a bug fix
1824887Schin	print hi
1834887Schin)
1844887Schin!
1854887Schinx=
1864887Schinfor j in  glob noglob
1874887Schindo	for i in 'a\*b' 'a\ b' 'a\bc' 'a\*b' 'a\"b'
1884887Schin	do	eval [[ '$('print -r -- \'$i\'\$x')' != "'$i'" ]]  && err_exit "quoting of $i\$x with $j enabled failed"
1894887Schin		eval [[ '$('print -r -- \'$i\'\${x%*}')' != "'$i'" ]]  && err_exit "quoting of $i\${x%*} with $j enabled failed"
1904887Schin		if	[[ $j == noglob ]]
1914887Schin		then	eval [[ '$('print -r -- \'$i\'\${x:-*}')' != "'$i''*'" ]]  && err_exit "quoting of $i\${x:-*} with $j enabled failed"
1924887Schin		fi
1934887Schin	done
1944887Schin	set -f
1954887Schindone
1964887Schinfoo=foo
1974887Schin[[ "$" == '$' ]] || err_exit '"$" != $'
1984887Schin[[ "${foo}$" == 'foo$' ]] || err_exit 'foo=foo;"${foo}$" != foo$'
1994887Schin[[ "${foo}${foo}$" == 'foofoo$' ]] || err_exit 'foo=foo;"${foo}${foo}$" != foofoo$'
20010898Sroland.mainz@nrubsig.orgfoo='$ '
2018462SApril.Chin@Sun.COM[[ "$foo" == ~(Elr)(\\\$|#)\  ]] || err_exit $'\'$ \' not matching RE \\\\\\$|#\''
20210898Sroland.mainz@nrubsig.org[[ "$foo" == ~(Elr)('\$'|#)\  ]] || err_exit $'\'$ \' not matching RE \'\\$\'|#\''
2038462SApril.Chin@Sun.COMfoo='# '
20410898Sroland.mainz@nrubsig.org[[ "$foo" == ~(Elr)(\\\$|#)\  ]] || err_exit $'\'# \' not matching RE \\'\$|#\''
2058462SApril.Chin@Sun.COM[[ "$foo" == ~(Elr)('\$'|#)\  ]] || err_exit $'\'# \' not matching RE \'\\$\'|#\''
2068462SApril.Chin@Sun.COM[[ '\$' == '\$'* ]] ||   err_exit $'\'\\$\' not matching \'\\$\'*'
20710898Sroland.mainz@nrubsig.org[[ a+a == ~(E)a\+a ]] || err_exit '~(E)a\+a not matching a+a'
20810898Sroland.mainz@nrubsig.org[[ a+a =~ a\+a ]] || err_exit 'RE a\+a not matching a+a'
2094887Schinexit $((Errors))
210