xref: /onnv-gate/usr/src/lib/libshell/common/tests/append.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
304887Schin{
314887Schinx=abc
324887Schinx+=def ;} 2> /dev/null
334887Schinif	[[ $x != abcdef ]]
344887Schinthen	err_exit 'abc+def != abcdef'
354887Schinfi
364887Schininteger i=3
374887Schin{ i+=4;} 2> /dev/null
384887Schinif	(( i != 7 ))
394887Schinthen	err_exit '3+4!=7'
404887Schinfi
414887Schiniarray=( one two three )
424887Schin{ iarray+= (four five six) ;} 2> /dev/null
434887Schinif	[[ ${iarray[@]} != 'one two three four five six' ]]
444887Schinthen	err_exit 'index array append fails'
454887Schinfi
464887Schinunset iarray
474887Schiniarray=one
484887Schin{ iarray+= (four five six) ;} 2> /dev/null
494887Schinif	[[ ${iarray[@]} != 'one four five six' ]]
504887Schinthen	err_exit 'index array append to scalar fails'
514887Schinfi
524887Schintypeset -A aarray
534887Schinaarray=( [1]=1 [3]=4 [xyz]=xyz )
544887Schinaarray+=( [2]=2 [3]=3 [foo]=bar )
554887Schinif	[[ ${aarray[3]} != 3 ]]
564887Schinthen	err_exit 'associative array append fails'
574887Schinfi
584887Schinif	[[ ${#aarray[@]} != 5 ]]
594887Schinthen	err_exit 'number of elements of associative array append fails'
604887Schinfi
614887Schinpoint=(x=1 y=2)
624887Schinpoint+=( y=3 z=4)
634887Schinif	[[ ${point.y} != 3 ]]
644887Schinthen	err_exit 'compound append fails'
654887Schinfi
668462SApril.Chin@Sun.COMif	[[ ${point.x} != 1 ]]
678462SApril.Chin@Sun.COMthen	err_exit 'compound append to compound variable unsets existing variables'
688462SApril.Chin@Sun.COMfi
694887Schinunset foo
704887Schinfoo=one
714887Schinfoo+=(two)
724887Schinif	[[ ${foo[@]} != 'one two' ]]
734887Schinthen	err_exit 'array append to non array variable fails'
744887Schinfi
758462SApril.Chin@Sun.COMunset foo
768462SApril.Chin@Sun.COMfoo[0]=(x=3)
778462SApril.Chin@Sun.COMfoo+=(x=4)
788462SApril.Chin@Sun.COM[[ ${foo[1].x} == 4 ]] || err_exit 'compound append to index array not working'
798462SApril.Chin@Sun.COM[[ ${foo[0].x} == 3 ]] || err_exit 'compound append to index array unsets existing variables'
804887Schinexit $((Errors))
81