xref: /onnv-gate/usr/src/lib/libshell/common/tests/quoting.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
304887Schinif	[[ 'hi there' != "hi there" ]]
314887Schinthen	err_exit "single quotes not the same as double quotes"
324887Schinfi
334887Schinx='hi there'
344887Schinif	[[ $x != 'hi there' ]]
354887Schinthen	err_exit "$x not the same as 'hi there'"
364887Schinfi
374887Schinif	[[ $x != "hi there" ]]
384887Schinthen	err_exit "$x not the same as \"hi there \""
394887Schinfi
404887Schinif	[[ \a\b\c\*\|\"\ \\ != 'abc*|" \' ]]
414887Schinthen	err_exit " \\ differs from '' "
424887Schinfi
434887Schinif	[[ "ab\'\"\$(" != 'ab\'\''"$(' ]]
444887Schinthen	err_exit " \"\" differs from '' "
454887Schinfi
464887Schinif	[[ $(print -r - 'abc*|" \') !=  'abc*|" \' ]]
474887Schinthen	err_exit "\$(print -r - '') differs from ''"
484887Schinfi
494887Schinif	[[ $(print -r - "abc*|\" \\") !=  'abc*|" \' ]]
504887Schinthen	err_exit "\$(print -r - '') differs from ''"
514887Schinfi
524887Schinif	[[ "$(print -r - 'abc*|" \')" !=  'abc*|" \' ]]
534887Schinthen	err_exit "\"\$(print -r - '')\" differs from ''"
544887Schinfi
554887Schinif	[[ "$(print -r - "abc*|\" \\")" !=  'abc*|" \' ]]
564887Schinthen	err_exit "\"\$(print -r - "")\" differs from ''"
574887Schinfi
584887Schinif	[[ $(print -r - $(print -r - 'abc*|" \')) !=  'abc*|" \' ]]
594887Schinthen	err_exit "nested \$(print -r - '') differs from ''"
604887Schinfi
614887Schinif	[[ "$(print -r - $(print -r - 'abc*|" \'))" !=  'abc*|" \' ]]
624887Schinthen	err_exit "\"nested \$(print -r - '')\" differs from ''"
634887Schinfi
644887Schinif	[[ $(print -r - "$(print -r - 'abc*|" \')") !=  'abc*|" \' ]]
654887Schinthen	err_exit "nested \"\$(print -r - '')\" differs from ''"
664887Schinfi
674887Schinunset x
684887Schinif	[[ ${x-$(print -r - "abc*|\" \\")} !=  'abc*|" \' ]]
694887Schinthen	err_exit "\${x-\$(print -r - '')} differs from ''"
704887Schinfi
714887Schinif	[[ ${x-$(print -r - "a}c*|\" \\")} !=  'a}c*|" \' ]]
724887Schinthen	err_exit "\${x-\$(print -r - '}')} differs from ''"
734887Schinfi
744887Schinx=$((echo foo)|(cat))
754887Schinif	[[ $x != foo  ]]
764887Schinthen	err_exit "((cmd)|(cmd)) failed"
774887Schinfi
784887Schinx=$(print -r -- "\"$HOME\"")
794887Schinif	[[ $x != '"'$HOME'"' ]]
804887Schinthen	err_exit "nested double quotes failed"
814887Schinfi
824887Schin: ${z="a{b}c"}
834887Schinif	[[ $z != 'a{b}c' ]]
844887Schinthen	err_exit '${z="a{b}c"} not correct'
854887Schinfi
864887Schinunset z
874887Schin: "${z="a{b}c"}"
884887Schinif	[[ $z != 'a{b}c' ]]
894887Schinthen	err_exit '"${z="a{b}c"}" not correct'
904887Schinfi
914887Schinif	[[ $(print -r -- "a\*b") !=  'a\*b' ]]
924887Schinthen	err_exit '$(print -r -- "a\*b") differs from  a\*b'
934887Schinfi
944887Schinunset x
954887Schinif	[[ $(print -r -- "a\*b$x") !=  'a\*b' ]]
964887Schinthen	err_exit '$(print -r -- "a\*b$x") differs from  a\*b'
974887Schinfi
984887Schinx=hello
994887Schinset -- ${x+foo bar bam}
1004887Schinif	(( $# !=3 ))
1014887Schinthen	err_exit '${x+foo bar bam} does not yield three arguments'
1024887Schinfi
1034887Schinset -- ${x+foo "bar bam"}
1044887Schinif	(( $# !=2 ))
1054887Schinthen	err_exit '${x+foo "bar bam"} does not yield two arguments'
1064887Schinfi
1074887Schinset -- ${x+foo 'bar bam'}
1084887Schinif	(( $# !=2 ))
1094887Schinthen	err_exit '${x+foo '\''bar bam'\''} does not yield two arguments'
1104887Schinfi
1114887Schinset -- ${x+foo $x bam}
1124887Schinif	(( $# !=3 ))
1134887Schinthen	err_exit '${x+foo $x bam} does not yield three arguments'
1144887Schinfi
1154887Schinset -- ${x+foo "$x" bam}
1164887Schinif	(( $# !=3 ))
1174887Schinthen	err_exit '${x+foo "$x" bam} does not yield three arguments'
1184887Schinfi
1194887Schinset -- ${x+"foo $x bam"}
1204887Schinif	(( $# !=1 ))
1214887Schinthen	err_exit '${x+"foo $x bam"} does not yield one argument'
1224887Schinfi
1234887Schinset -- "${x+foo $x bam}"
1244887Schinif	(( $# !=1 ))
1254887Schinthen	err_exit '"${x+foo $x bam}" does not yield one argument'
1264887Schinfi
1274887Schinset -- ${x+foo "$x "bam}
1284887Schinif	(( $# !=2 ))
1294887Schinthen	err_exit '${x+foo "$x "bam} does not yield two arguments'
1304887Schinfi
1314887Schinx="ab$'cd"
1324887Schinif	[[ $x != 'ab$'"'cd" ]]
1334887Schinthen	err_exit '$'"' inside double quotes not working"
1344887Schinfi
1354887Schinx=`print 'ab$'`
1364887Schinif	[[ $x != 'ab$' ]]
1374887Schinthen	err_exit '$'"' inside `` quotes not working"
1384887Schinfi
1394887Schinunset a
1404887Schinx=$(print -r -- "'\
1414887Schin\
1424887Schin")
1434887Schinif	[[ $x != "'" ]]
1444887Schinthen	err_exit 'line continuation in double strings not working'
1454887Schinfi
1464887Schinx=$(print -r -- "'\
1474887Schin$a\
1484887Schin")
1494887Schinif	[[ $x != "'" ]]
1504887Schinthen	err_exit 'line continuation in expanded double strings not working'
1514887Schinfi
1524887Schinx='\*'
1534887Schinif	[[ $(print -r -- $x) != '\*' ]]
1544887Schinthen	err_exit 'x="\\*";$x != \*'
1554887Schinfi
1564887Schinx='   hello    world    '
1574887Schinset -- $x
1584887Schinif	(( $# != 2 ))
1594887Schinthen	err_exit 'field splitting error'
1604887Schinfi
1614887Schinx=$(print -r -- '1234567890123456789012345678901234567890123456789012345678901234567890 \
1624887Schin1234567890123456789012345678901234567890123456789012345678901234567890 \
1634887Schin1234567890123456789012345678901234567890123456789012345678901234567890 \
1644887Schin1234567890123456789012345678901234567890123456789012345678901234567890 \
1654887Schin1234567890123456789012345678901234567890123456789012345678901234567890 \
1664887Schin1234567890123456789012345678901234567890123456789012345678901234567890 \
1674887Schin1234567890123456789012345678901234567890123456789012345678901234567890 \
1684887Schin1234567890123456789012345678901234567890123456789012345678901234567890 \
1694887Schin1234567890123456789012345678901234567890123456789012345678901234567890 \
1704887Schin1234567890123456789012345678901234567890123456789012345678901234567890 \
1714887Schin1234567890123456789012345678901234567890123456789012345678901234567890 \
1724887Schin1234567890123456789012345678901234567890123456789012345678901234567890 \
1734887Schin1234567890123456789012345678901234567890123456789012345678901234567890 \
1744887Schin1234567890123456789012345678901234567890123456789012345678901234567890 \
1754887Schin1234567890123456789012345678901234567890123456789012345678901234567890')
1764887Schinif	(( ${#x} != (15*73-3) ))
1774887Schinthen	err_exit "length of x, ${#x}, is incorrect should be $((15*73-3))"
1784887Schinfi
1794887Schinx='$hi'
1804887Schinif	[[ $x\$ != '$hi$' ]]
1814887Schinthen	err_exit ' $x\$, with x=$hi, does not expand to $hi$'
1824887Schinfi
1834887Schinif	[[ $x$ != '$hi$' ]]
1844887Schinthen	err_exit ' $x$, with x=$hi, does not expand to $hi$'
1854887Schinfi
1864887Schinset -- $(/bin/echo foo;sleep 1;/bin/echo bar)
1874887Schinif	[[ $# != 2 ]]
1884887Schinthen	err_exit 'word splitting after command substitution not working'
1894887Schinfi
1904887Schinunset q
1914887Schinif	[[ "${q:+'}q${q:+'}" != q ]]
1924887Schinthen	err_exit 'expansion of "{q:+'\''}" not correct when q unset'
1934887Schinfi
1944887Schinq=1
1954887Schinif	[[ "${q:+'}q${q:+'}" != "'q'" ]]
1964887Schinthen	err_exit 'expansion of "{q:+'\''}" not correct when q set'
1974887Schinfi
1984887Schinx=$'x\' #y'
1994887Schinif	[[ $x != "x' #y" ]]
2004887Schinthen	err_exit "$'x\' #y'" not working
2014887Schinfi
2024887Schinx=$q$'x\' #y'
2034887Schinif	[[ $x != "1x' #y" ]]
2044887Schinthen	err_exit "$q$'x\' #y'" not working
2054887Schinfi
2064887SchinIFS=,
2074887Schinx='a,b\,c,d'
2084887Schinset -- $x
2094887Schinif	[[ $2 != 'b\' ]]
2104887Schinthen	err_exit "field splitting of $x with IFS=$IFS not working"
2114887Schinfi
2124887Schinfoo=bar
2134887Schinbar=$(print -r -- ${foo+\\n\ })
2144887Schinif	[[ $bar != '\n ' ]]
21510898Sroland.mainz@nrubsig.orgthen	err_exit '${foo+\\n\ } expansion error'
2164887Schinfi
2174887Schinunset bar
2184887Schinbar=$(print -r -- ${foo+\\n\ $bar})
2194887Schinif	[[ $bar != '\n ' ]]
22010898Sroland.mainz@nrubsig.orgthen	err_exit '${foo+\\n\ $bar} expansion error with bar unset'
2214887Schinfi
2224887Schinx='\\(..\\)|&\|\|\\&\\|'
2234887Schinif	[[ $(print -r -- $x) != "$x" ]]
2244887Schinthen	err_exit '$x, where x=\\(..\\)|&\|\|\\&\\| not working'
2254887Schinfi
2264887Schinx='\\('
2274887Schinif	[[ $(print -r -- a${x}b) != a"${x}"b ]]
2284887Schinthen	err_exit 'a${x}b, where x=\\( not working'
2294887Schinfi
2304887Schinx=
2314887Schinif	[[ $(print -r -- $x'\\1') != '\\1' ]]
2324887Schinthen	err_exit 'backreference inside single quotes broken'
2334887Schinfi
2344887Schinset -- ''
2354887Schinset -- "$@"
2364887Schinif	(( $# != 1 ))
2374887Schinthen	err_exit '"$@" not preserving nulls'
2384887Schinfi
2394887Schinx=
2404887Schinif	[[ $(print -r s"!\2${x}\1\a!") != 's!\2\1\a!' ]]
2414887Schinthen	err_exit  'print -r s"!\2${x}\1\a!" not equal s!\2\1\a!'
2424887Schinfi
2434887Schinif	[[ $(print -r $'foo\n\n\n') != foo ]]
2444887Schinthen	err_exit 'trailing newlines on comsubstitution not removed'
2454887Schinfi
2464887Schinunset x
2474887Schinif	[[ ${x:='//'} != '//' ]]
2484887Schinthen	err_exit '${x:='//'} != "//"'
2494887Schinfi
2504887Schinif	[[ $(print -r "\"hi$\"")	!= '"hi$"' ]]
2514887Schinthen	err_exit '$\ not correct inside ""'
2524887Schinfi
2534887Schinunset x
2544887Schinif	[[ "${x-a\}b}" != 'a}b' ]]
2554887Schinthen	err_exit '"${x-a\}b}" !=  "a}b"'
2564887Schinfi
2574887Schinif	[[ "\}\]$x\*\{\[\\" != '\}\]\*\{\[\' ]]
2584887Schinthen	err_exit '"\}\]$x\*\{\[\\" !=  "\}\]\*\{\[\"'
2594887Schinfi
2604887Schinfoo=yes
2614887Schinif	[[ $(print -r -- {\$foo}) != '{$foo}' ]]
2624887Schinthen	err_exit '{\$foo}' not expanded correctly
2634887Schinfi
2644887Schin[[ foo == $(
2654887Schin###########################################################
2664887Schin###########################################################
2674887Schin###########################################################
2684887Schin###########################################################
2694887Schin###########################################################
2704887Schin###########################################################
2714887Schin###########################################################
2724887Schin###########################################################
2734887Schin###########################################################
2744887Schin###########################################################
2754887Schin###########################################################
2764887Schin###########################################################
2774887Schin###########################################################
2784887Schin###########################################################
2794887Schin###########################################################
2804887Schin###########################################################
2814887Schin###########################################################
2824887Schin###########################################################
2834887Schin###########################################################
2844887Schin###########################################################
2854887Schin###########################################################
2864887Schin###########################################################
2874887Schin###########################################################
2884887Schin###########################################################
2894887Schin###########################################################
2904887Schin###########################################################
2914887Schin###########################################################
2924887Schin###########################################################
2934887Schin###########################################################
2944887Schin###########################################################
2954887Schin###########################################################
2964887Schin###########################################################
2974887Schin###########################################################
2984887Schin###########################################################
2994887Schin###########################################################
3004887Schin###########################################################
3014887Schin###########################################################
3024887Schin###########################################################
3034887Schinprint foo) ]] || err_exit "command subsitution with long comments broken"
3044887Schinsubject='some/other/words'
3054887Schinre='(?*)/(?*)/(?*)'
3064887Schin[[ ${subject/${re}/\3} != words ]] && err_exit 'string replacement with \3 not working'
3074887Schin[[ ${subject/${re}/'\3'} != '\3' ]] && err_exit 'string replacement with '"'\3'"' not working'
3084887Schin[[ ${subject/${re}/"\\3"} != '\3' ]] && err_exit 'string replacement with "\\3" not working'
3094887Schin[[ ${subject/${re}/"\3"} != '\3' ]] && err_exit 'string replacement with "\3" not working'
3104887Schinstring='\3'
3114887Schin[[ ${subject/${re}/${string}} != words ]] && err_exit 'string replacement with $string not working with string=\3'
3124887Schin[[ $(print -r "${subject/${re}/${string}}") != words ]] && err_exit 'string replacement with $string not working with string=\3 using print'
3134887Schin[[ ${subject/${re}/"${string}"} != '\3' ]] && err_exit 'string replacement with "$string" not working with  string=\3'
3144887Schin[[ $(print -r "${subject/${re}/"${string}"}") != '\3' ]] && err_exit 'string replacement with "$string" not working with  string=\3 using print'
3154887Schinstring='\\3'
3164887Schin[[ ${subject/${re}/${string}} != '\3' ]] && err_exit 'string replacement with $string not working with string=\\3'
3174887Schin[[ ${subject/${re}/"${string}"} != '\\3' ]] && err_exit 'string replacement with "$string" not working with  string=\\3'
3184887Schin[[ ${subject/${re}/\4} != '\4' ]] && err_exit 'string replacement with \4 not working'
3194887Schin[[ ${subject/${re}/'\4'} != '\4' ]] && err_exit 'string replacement with '\4' not working'
3204887Schinstring='\4'
3214887Schin[[ ${subject/${re}/${string}} != '\4' ]] && err_exit 'string replacement with $string not working with string=\4'
3224887Schin[[ ${subject/${re}/"${string}"} != '\4' ]] && err_exit 'string replacement with "$string" not working with  string=\4'
3234887Schinstring='&foo'
3244887Schin[[ ${subject/${re}/${string}} != '&foo' ]] && err_exit 'string replacement with $string not working with string=&foo'
3254887Schin[[ ${subject/${re}/"${string}"} != '&foo' ]] && err_exit 'string replacement with "$string" not working with  string=&foo'
3264887Schin{
3274887Schinx=x
3284887Schinx=${x:-`id | sed 's/^[^(]*(\([^)]*\)).*/\1/'`}
32910898Sroland.mainz@nrubsig.org} 2> /dev/null || err_exit 'skipping over `` failed'
3304887Schin[[ $x == x ]] || err_exit 'assignment ${x:=`...`} failed'
3318462SApril.Chin@Sun.COM[[ $($SHELL -c 'print a[') == 'a[' ]] || err_exit "unbalanced '[' in command arg fails"
3328462SApril.Chin@Sun.COM$SHELL -c $'false && (( `wc -l /dev/null | nawk \'{print $1}\'` > 2 )) && true;:' 2> /dev/null ||  err_exit 'syntax error with ` in arithmetic expression'
3338462SApril.Chin@Sun.COM{ $SHELL  -c '((  1`: "{ }"` ))' ;} 2> /dev/null || err_exit 'problem with ` inside (())'
3348462SApril.Chin@Sun.COMvarname=foobarx
3358462SApril.Chin@Sun.COMx=`print '"\$'${varname}'"'`
3368462SApril.Chin@Sun.COM[[ $x == '"$foobarx"' ]] ||  err_exit $'\\$\' not handled correctly inside ``'
33710898Sroland.mainz@nrubsig.org
33810898Sroland.mainz@nrubsig.orgcopy1=5 copynum=1
33910898Sroland.mainz@nrubsig.orgfoo="`eval echo "$"{copy$copynum"-0}"`"
34010898Sroland.mainz@nrubsig.org[[ $foo == "$copy1" ]] || err_exit '$"..." not being ignored inside ``'
34110898Sroland.mainz@nrubsig.org
3424887Schinexit $((Errors))
343