1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
|
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Filter stdin to remove builtin array variables that are
# automatically set and possibly contain values that vary.
ignore_builtin_arrays()
{
grep -v -e BASH_VERSINFO= -e PIPESTATUS= -e GROUPS=
}
# this is needed so that the bad assignments (b[]=bcde, for example) do not
# cause fatal shell errors when in posix mode
set +o posix
set +a
# first make sure we handle the basics
x=()
echo ${x[@]}
unset x
# this should be an error
test=(first & second)
echo $?
unset test
# make sure declare -a converts an existing variable to an array
unset a
a=abcde
declare -a a
echo ${a[0]}
if [ "$a" != "${a[0]}" ]; then
echo 'array.tests: $a and $a[0] array mismatch'
fi
unset a
a=abcde
a[2]=bdef
unset b
declare -a b[256]
unset c[2]
unset c[*]
a[1]=
_ENV=/bin/true
x=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]}
declare -r c[100]
echo ${a[0]} ${a[4]}
echo ${a[@]}
echo ${a[*]}
# this should print out values, too
declare -a | ignore_builtin_arrays
unset a[7]
echo ${a[*]}
unset a[4]
echo ${a[*]}
echo ${a}
echo "${a}"
echo $a
unset a[0]
echo ${a}
echo ${a[@]}
a[5]="hello world"
echo ${a[5]}
echo ${#a[5]}
echo ${#a[@]}
a[4+5/2]="test expression"
declare a["7 + 8"]="test 2"
a[7 + 8]="test 2"
echo ${a[@]}
readonly a[5]
readonly a
# these two lines should output `declare' commands
readonly -a | ignore_builtin_arrays
declare -ar | ignore_builtin_arrays
# this line should output `readonly' commands, even for arrays
set -o posix
readonly -a | ignore_builtin_arrays
set +o posix
declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")'
d[9]="ninth element"
declare -a e[10]=test # this works in post-bash-2.05 versions
declare -a e[10]='(test)'
pass=/etc/passwd
declare -a f='("${d[@]}")'
b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass)
echo ${b[@]:2:3}
declare -pa | ignore_builtin_arrays
a[3]="this is a test"
b[]=bcde
b[*]=aaa
echo ${b[ ]}
c[-2]=4
echo ${c[-4]}
d[7]=(abdedfegeee)
d=([]=abcde [1]="test test" [*]=last [-65]=negative )
d=([0]=abcde [1]="test test" [*]=last )
d=([1]="test test" [-65]=negative )
unset d[12]
unset e[*]
declare -a | ignore_builtin_arrays
ps1='hello'
unset ps1[2]
unset ${ps1[2]}
declare +a ps1
declare +a c
# the prompt should not print when using a here doc
read -p "array test: " -a rv <<!
this is a test of read using arrays
!
echo ${rv[0]} ${rv[4]}
echo ${rv[@]}
# the variable should be converted to an array when `read -a' is done
vv=1
read -a vv <<!
this is a test of arrays
!
echo ${vv[0]} ${vv[3]}
echo ${vv[@]}
unset vv
declare -a | ignore_builtin_arrays
export rv
#set
x[4]=bbb
x=abde
echo $x
echo ${x[0]}
echo ${x[4]}
echo efgh | ( read x[1] ; echo ${x[1]} )
echo wxyz | ( declare -a x ; read x ; echo $x ; echo ${x[0]} )
# Make sure that arrays can be used to save the positional parameters verbatim
set -- a 'b c' d 'e f g' h
ARGV=( [0]=$0 "$@" )
for z in "${ARGV[@]}"
do
echo "$z"
done
echo "$0"
for z in "$@"
do
echo "$z"
done
# do various pattern removal and length tests
XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
xpath=( $( IFS=: ; echo $XPATH ) )
echo ${xpath[@]}
echo ${xpath[@]##*/}
echo ${xpath[0]##*/}
echo ${xpath[@]%%[!/]*}
echo ${xpath[0]%%[!/]*}
recho ${xpath##*/}
recho ${xpath%%[!/]*}
recho ${xpath[5]##*/}
recho ${xpath[5]%%[!/]*}
# let's try to make it a DOS-style path
zecho "${xpath[@]/\//\\}"
zecho "${xpath[@]//\//\\}"
zecho "${xpath[@]//[\/]/\\}"
# length of the first element of the array, since array without subscript
# is equivalent to referencing first element
echo ${#xpath} -- ${#xpath[0]}
# number of elements in the array
nelem=${#xpath[@]}
echo ${#xpath[@]} -- $nelem
# total length of all elements in the array, including space separators
xx="${xpath[*]}"
echo ${#xx}
# total length of all elements in the array
xx=$( IFS='' ; echo "${xpath[*]}" )
echo ${#xx}
unset xpath[nelem-1]
nelem=${#xpath[@]}
echo ${#xpath[@]} -- $nelem
# error
echo ${#xpath[-10]}
# zero length for unset elements
echo ${#xpath[42]}
# arrays and things that look like index assignments
array=(42 [1]=14 [2]=44)
array2=(grep [ 123 ] \*)
echo ${array[@]}
echo "${array2[@]}"
# arrays and implicit arithmetic evaluation
declare -i -a iarray
iarray=( 2+4 1+6 7+2 )
echo ${iarray[@]}
iarray[4]=4+1
echo ${iarray[@]}
# make sure assignment using the compound assignment syntax removes all
# of the old elements from the array value
barray=(old1 old2 old3 old4 old5)
barray=(new1 new2 new3)
echo "length = ${#barray[@]}"
echo "value = ${barray[*]}"
# the compound assignment syntax inherited from ksh93 has some quirks
badarray=( metacharacters like & need to be quoted in compound assignments)
# make sure the array code behaves correctly with respect to unset variables
set -u
( echo ${#narray[4]} )
${THIS_SH} ./array1.sub
${THIS_SH} ./array2.sub
# some old bugs and ksh93 compatibility tests
${THIS_SH} ./array3.sub
# some compound assignment parsing problems that showed up in bash-3.1-release
${THIS_SH} ./array4.sub
set +u
cd ${TMPDIR:=/tmp}
touch 1=bar
foo=([10]="bar")
echo ${foo[0]}
rm 1=bar
cd $OLDPWD
foo=(a b c d e f g)
echo ${foo[@]}
# quoted reserved words are ok
foo=(\for \case \if \then \else)
echo ${foo[@]}
# quoted metacharacters are ok
foo=( [1]='<>' [2]='<' [3]='>' [4]='!' )
echo ${foo[@]}
# numbers are just words when not in a redirection context
foo=( 12 14 16 18 20 )
echo ${foo[@]}
foo=( 4414758999202 )
echo ${foo[@]}
# this was a bug in all versions of bash 2.x up to and including bash-2.04
declare -a ddd=(aaa
bbb)
echo ${ddd[@]}
# errors until post-bash-2.05a; now reserved words are OK
foo=(a b c for case if then else)
foo=(for case if then else)
# errors
metas=( <> < > ! )
metas=( [1]=<> [2]=< [3]=> [4]=! )
# various expansions that didn't really work right until post-bash-2.04
foo='abc'
echo ${foo[0]} ${#foo[0]}
echo ${foo[1]} ${#foo[1]}
echo ${foo[@]} ${#foo[@]}
echo ${foo[*]} ${#foo[*]}
foo=''
echo ${foo[0]} ${#foo[0]}
echo ${foo[1]} ${#foo[1]}
echo ${foo[@]} ${#foo[@]}
echo ${foo[*]} ${#foo[*]}
# new expansions added after bash-2.05b
x[0]=zero
x[1]=one
x[4]=four
x[10]=ten
recho ${!x[@]}
recho "${!x[@]}"
recho ${!x[*]}
recho "${!x[*]}"
# sparse array tests for code fixed in bash-3.0
unset av
av[1]='one'
av[2]=''
av[3]=three
av[5]=five
av[7]=seven
echo include null element -- expect one
echo ${av[@]:1:2} # what happens when we include a null element?
echo include unset element -- expect three five
echo ${av[@]:3:2} # what happens when we include an unset element?
echo start at unset element -- expect five seven
echo ${av[@]:4:2} # what happens when we start at an unset element?
echo too many elements -- expect three five seven
echo ${av[@]:3:5} # how about too many elements?
echo positive offset - expect five seven
echo ${av[@]:5:2}
echo negative offset to unset element - expect seven
echo ${av[@]: -2:2}
echo positive offset 2 - expect seven
echo ${av[@]: 6:2}
echo negative offset 2 - expect seven
echo ${av[@]: -1:2}
echo out-of-range offset
echo ${av[@]:12}
# parsing problems and other inconsistencies not fixed until post bash-3.0
unset x
declare -a x=(')' $$)
[ ${x[1]} -eq $$ ] || echo bad
unset x
declare -a x=(a b c d e)
echo ${x[4]}
z=([1]=one [4]=four [7]=seven [10]=ten)
echo ${#z[@]}
echo ${!z[@]}
unset x
declare -a x=(a \'b c\')
echo "${x[1]}"
unset x
declare -a x=(a 'b c')
echo "${x[1]}"
unset x
declare -a x=($0)
[ "${x[@]}" = $0 ] || echo double expansion of \$0
declare -a x=(\$0)
echo "${x[@]}"
unset A Z
Z='a b'
A=( X=$Z )
declare -p A
# combine with brace expansion
letters=( {0..9} )
echo "${letters["{2..6}"]}"
# tests for assigning to noassign array variables
BASH_ARGC=(xxx) ; echo FIN1:$?
BASH_ARGC=foio ; echo FIN2:$?
declare BASH_ARGC=(xxx) ; echo FIN3:$?
declare BASH_ARGC=foio ; echo FIN4:$?
BASH_ARGV[1]=foo ; echo FIN5:$?
declare BASH_ARGV[1]=foo ; echo FIN6:$?
# tests for bash-3.1 problems
${THIS_SH} ./array5.sub
# tests for post-bash-3.2 problems, most fixed in bash-3.2 patches
${THIS_SH} ./array6.sub
${THIS_SH} ./array7.sub
${THIS_SH} ./array8.sub
${THIS_SH} ./array9.sub
${THIS_SH} ./array10.sub
${THIS_SH} ./array11.sub
${THIS_SH} ./array12.sub
${THIS_SH} ./array13.sub
${THIS_SH} ./array14.sub
${THIS_SH} ./array15.sub
${THIS_SH} ./array16.sub
${THIS_SH} ./array17.sub
${THIS_SH} ./array18.sub
${THIS_SH} ./array19.sub
${THIS_SH} ./array20.sub
${THIS_SH} ./array21.sub
${THIS_SH} ./array22.sub
${THIS_SH} ./array23.sub
${THIS_SH} ./array24.sub
${THIS_SH} ./array25.sub
${THIS_SH} ./array26.sub
${THIS_SH} ./array27.sub
${THIS_SH} ./array28.sub
${THIS_SH} ./array29.sub
${THIS_SH} ./array30.sub
${THIS_SH} ./array31.sub
${THIS_SH} ./array32.sub
${THIS_SH} ./array33.sub
|