⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 array.tests

📁 android-w.song.android.widget
💻 TESTS
字号:
# this is needed so that the bad assignments (b[]=bcde, for example) do not# cause fatal shell errors when in posix modeset +o posixset +a# The calls to egrep -v are to filter out builtin array variables that are# automatically set and possibly contain values that vary.# first make sure we handle the basicsx=()echo ${x[@]}unset x# this should be an errortest=(first & second)echo $?unset test# make sure declare -a converts an existing variable to an arrayunset aa=abcdedeclare -a aecho ${a[0]}unset aa=abcdea[2]=bdefunset bdeclare -a b[256]unset c[2]unset c[*]a[1]=_ENV=/bin/truex=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]}declare -r c[100]echo ${a[0]} ${a[4]}echo ${a[@]}echo ${a[*]}# this should print out values, toodeclare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'unset a[7]echo ${a[*]}unset a[4]echo ${a[*]}echo ${a}echo "${a}"echo $aunset 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' commandsreadonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'declare -ar | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'# this line should output `readonly' commands, even for arraysset -o posixreadonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'set +o posixdeclare -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 versionsdeclare -a e[10]='(test)'pass=/etc/passwddeclare -a f='("${d[@]}")'b=([0]=this [1]=is [2]=a [3]=test [4]="$PS1" [5]=$pass)echo ${b[@]:2:3}declare -pa | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'a[3]="this is a test"b[]=bcdeb[*]=aaaecho ${b[   ]}c[-2]=4echo ${c[-4]}d[7]=(abdedfegeee)d=([]=abcde [1]="test test" [*]=last [-65]=negative )unset d[12]unset e[*]declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'ps1='hello'unset ps1[2]unset ${ps1[2]}declare +a ps1declare +a c# the prompt should not print when using a here docread -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 donevv=1read -a vv <<!this is a test of arrays!echo ${vv[0]} ${vv[3]}echo ${vv[@]}unset vvdeclare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'export rv#setx[4]=bbbx=abdeecho $xecho ${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 paramters verbatimset -- a 'b c' d 'e f g' hARGV=( [0]=$0 "$@" )for z in "${ARGV[@]}"do	echo "$z"doneecho "$0"for z in "$@"do	echo "$z"done# do various pattern removal and length testsXPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbinxpath=( $( 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 pathzecho "${xpath[@]/\//\\}"zecho "${xpath[@]//\//\\}"zecho "${xpath[@]//[\/]/\\}"# length of the first element of the array, since array without subscript# is equivalent to referencing first elementecho ${#xpath} -- ${#xpath[0]}# number of elements in the arraynelem=${#xpath[@]}echo ${#xpath[@]} -- $nelem# total length of all elements in the array, including space separatorsxx="${xpath[*]}"echo ${#xx}# total length of all elements in the arrayxx=$( IFS='' ; echo "${xpath[*]}" )echo ${#xx}unset xpath[nelem-1]nelem=${#xpath[@]}echo ${#xpath[@]} -- $nelem# arrays and things that look like index assignmentsarray=(42 [1]=14 [2]=44)array2=(grep [ 123 ] \*)echo ${array[@]}echo "${array2[@]}"# arrays and implicit arithmetic evaluationdeclare -i -a iarrayiarray=( 2+4 1+6 7+2 )echo ${iarray[@]}iarray[4]=4+1echo ${iarray[@]}# make sure assignment using the compound assignment syntax removes all# of the old elements from the array valuebarray=(old1 old2 old3 old4 old5)barray=(new1 new2 new3)echo "length = ${#barray[@]}"echo "value = ${barray[*]}"# make sure the array code behaves correctly with respect to unset variablesset -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 assingment parsing problems that showed up in bash-3.1-release${THIS_SH} ./array4.subset +ucd /tmptouch 1=barfoo=([10]="bar")echo ${foo[0]}rm 1=barcd $OLDPWDfoo=(a b c d e f g)echo ${foo[@]}# quoted reserved words are okfoo=(\for \case \if \then \else)echo ${foo[@]}# quoted metacharacters are okfoo=( [1]='<>' [2]='<' [3]='>' [4]='!' )echo ${foo[@]}# numbers are just words when not in a redirection contextfoo=( 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.04declare -a ddd=(aaabbb)echo ${ddd[@]}# errors until post-bash-2.05a; now reserved words are OKfoo=(a b c for case if then else)foo=(for case if then else)# errorsmetas=( <> < > ! )metas=( [1]=<> [2]=< [3]=> [4]=! )# various expansions that didn't really work right until post-bash-2.04foo='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.05bx[0]=zerox[1]=onex[4]=fourx[10]=tenrecho ${!x[@]}recho "${!x[@]}"recho ${!x[*]}recho "${!x[*]}"# sparse array tests for code fixed in bash-3.0unset avav[1]='one'av[2]=''av[3]=threeav[5]=fiveav[7]=sevenecho include null element -- expect oneecho ${av[@]:1:2}	# what happens when we include a null element?echo include unset element -- expect three fiveecho ${av[@]:3:2}	# what happens when we include an unset element?echo start at unset element -- expect five sevenecho ${av[@]:4:2}	# what happens when we start at an unset element?echo too many elements -- expect three five sevenecho ${av[@]:3:5}	# how about too many elements?echo positive offset - expect five sevenecho ${av[@]:5:2}echo negative offset to unset element - expect sevenecho ${av[@]: -2:2}echo positive offset 2 - expect sevenecho ${av[@]: 6:2}echo negative offset 2 - expect sevenecho ${av[@]: -1:2}echo out-of-range offsetecho ${av[@]:12}# parsing problems and other inconsistencies not fixed until post bash-3.0unset xdeclare -a x=(')' $$)[ ${x[1]} -eq $$ ] || echo badunset xdeclare -a x=(a b c d e)echo ${x[4]}z=([1]=one [4]=four [7]=seven [10]=ten)echo ${#z[@]}echo ${!z[@]}unset xdeclare -a x=(a \'b  c\')echo "${x[1]}"unset xdeclare -a x=(a 'b  c')echo "${x[1]}"unset xdeclare -a x=($0)[ "${x[@]}" = $0 ] || echo double expansion of \$0declare -a x=(\$0)echo "${x[@]}"# 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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -