commit bash-20080626 snapshot

This commit is contained in:
Chet Ramey
2011-12-07 09:25:06 -05:00
parent e33f22038c
commit fdf670eaa1
48 changed files with 4104 additions and 1876 deletions
+8
View File
@@ -266,3 +266,11 @@ argv[1] = <element1 with spaces>
argv[2] = <element2 with spaces>
argv[1] = <element1 with spaces>
argv[2] = <element2 with spaces>
nord!olz
rdholz
rdholz
rdho
+1
View File
@@ -379,3 +379,4 @@ ${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
+381
View File
@@ -0,0 +1,381 @@
# 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
# 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 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]}
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 | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
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"
echo ${a[@]}
readonly a[5]
readonly a
# these two lines should output `declare' commands
readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
declare -ar | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
# this line should output `readonly' commands, even for arrays
set -o posix
readonly -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
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 | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
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 )
unset d[12]
unset e[*]
declare -a | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
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 | egrep -v '(BASH_VERSINFO|PIPESTATUS|GROUPS)'
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 paramters 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
# 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[*]}"
# 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 assingment parsing problems that showed up in bash-3.1-release
${THIS_SH} ./array4.sub
set +u
cd /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[@]}"
# 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
+14
View File
@@ -0,0 +1,14 @@
# these didn't work in versions of bash before bash-4.0
LNAME=nordholz
echo ${LNAME[$(( 0 ))]//h/!}
echo ${LNAME[$(( 2 ))]//h/!}
echo ${LNAME[$(( 0 ))]##??}
echo ${LNAME[$(( 2 ))]##??}
echo ${LNAME[$(( 0 ))]:2}
echo ${LNAME[$(( 0 ))]:2:4}
echo ${LNAME[$(( 2 ))]:2}
echo ${LNAME[$(( 2 ))]:2:4}
+101
View File
@@ -0,0 +1,101 @@
declare -A BASH_ALIASES='()'
declare -A BASH_CMDS='()'
declare -A fluff='()'
declare -A BASH_ALIASES='()'
declare -A BASH_CMDS='()'
declare -A fluff='([bar]="two" [foo]="one" )'
declare -A fluff='([bar]="two" [foo]="one" )'
declare -A fluff='([bar]="two" )'
declare -A fluff='([bar]="newval" )'
./assoc.tests: line 24: chaff: four: must use subscript when assigning associative array
declare -A BASH_ALIASES='()'
declare -A BASH_CMDS='()'
declare -Ai chaff='([one]="10" [zero]="5" )'
declare -Ar waste='([version]="4.0-devel" [source]="./assoc.tests" [lineno]="26" [pid]="42134" )'
declare -A wheat='([one]="a" [zero]="0" [two]="b" [three]="c" )'
declare -A chaff='([one]="10" [hello world]="flip" [zero]="5" )'
./assoc.tests: line 36: unset: waste: cannot unset: readonly variable
./assoc.tests: line 37: chaff[*]: bad array subscript
./assoc.tests: line 38: [*]=12: invalid associative array key
declare -A chaff='([one]="a" [hello world]="flip" )'
flip
argv[1] = <a>
argv[2] = <flip>
argv[3] = <multiple>
argv[4] = <words>
argv[1] = <a>
argv[2] = <flip>
argv[3] = <multiple words>
argv[1] = <a>
argv[2] = <flip>
argv[3] = <multiple>
argv[4] = <words>
argv[1] = <a flip multiple words>
./assoc.tests: line 55: declare: chaff: cannot destroy array variables in this way
./assoc.tests: line 57: chaff[*]: bad array subscript
./assoc.tests: line 58: [*]=12: invalid associative array key
declare -A wheat='([six]="6" [foo bar]="qux qix" )'
argv[1] = <qux>
argv[2] = <qix>
argv[1] = <qux qix>
declare -A wheat='([six]="6" [foo bar]="qux qix" )'
argv[1] = <2>
argv[1] = <7>
argv[1] = <qux>
argv[2] = <qix>
argv[3] = <blat>
argv[1] = <qux qix blat>
argv[1] = <16>
argv[1] = <16>
argv[1] = <flix>
argv[2] = <6>
argv[1] = <six>
argv[2] = <foo>
argv[3] = <bar>
argv[1] = <six>
argv[2] = <foo bar>
8
/usr/local/bin . /bin /sbin /usr/sbin /usr/bin /bin /usr/ucb
bin . bin sbin sbin bin bin ucb
bin
/ / / / / / /
/
argv[1] = <bin>
argv[1] = </>
argv[1] = <sbin>
argv[1] = </>
8
/usr/local/bin . /bin /sbin /usr/sbin /usr/bin /bin /usr/ucb
bin . bin sbin sbin bin bin ucb
/ / / / / / /
8
4 -- /bin
^usr^local^bin . ^bin ^sbin ^usr^sbin ^usr^bin ^bin ^usr^ucb
^usr^local^bin . ^bin ^sbin ^usr^sbin ^usr^bin ^bin ^usr^ucb
\usr/local/bin . \bin \sbin \usr/sbin \usr/bin \bin \usr/ucb
\usr\local\bin . \bin \sbin \usr\sbin \usr\bin \bin \usr\ucb
\usr\local\bin . \bin \sbin \usr\sbin \usr\bin \bin \usr\ucb
qux foo
/usr/local/bin/qux /usr/sbin/foo
hits command
0 /sbin/blat
0 /usr/local/bin/qux
0 /bin/sh
0 /usr/sbin/foo
blat qux sh foo
/sbin/blat /usr/local/bin/qux /bin/sh /usr/sbin/foo
foo qux
argv[1] = </usr/sbin/foo>
argv[2] = </usr/local/bin/qux>
argv[3] = <-l>
alias blat='cd /blat ; echo $PWD'
alias foo='/usr/sbin/foo'
alias qux='/usr/local/bin/qux -l'
alias sh='/bin/bash --login -o posix'
sh foo blat qux
argv[1] = </bin/bash --login -o posix>
argv[2] = </usr/sbin/foo>
argv[3] = <cd /blat ; echo $PWD>
argv[4] = </usr/local/bin/qux -l>
+169
View File
@@ -0,0 +1,169 @@
# TEST - basic declaration and assignment
typeset -A fluff
declare -A
fluff[foo]=one
fluff[bar]=two
declare -A
declare -p fluff
unset fluff[foo]
declare -p fluff
fluff[bar]=newval
declare -p fluff
unset fluff
# TEST - compount assignment and variable attributes
declare -A wheat chaff
wheat=( [zero]=0 [one]=a [two]=b [three]=c )
declare -i chaff
chaff=( [zero]=1+4 [one]=3+7 four )
declare -A waste=( [pid]=42134 [version]=4.0-devel [source]=$0 [lineno]=$LINENO )
declare -r waste
declare -A
declare +i chaff
chaff[hello world]=flip
declare -p chaff
# TEST - errors
unset waste
chaff[*]=12
chaff=( [one]=a [*]=12 )
# TEST - key expansion -- no word splitting
chaff[hello world]=flip
declare -p chaff
echo ${chaff[hello world]}
chaff[box]="multiple words"
recho ${chaff[@]}
recho "${chaff[@]}"
recho ${chaff[*]}
recho "${chaff[*]}"
unset chaff
declare -A chaff[200]
declare +A chaff
chaff[*]=12
chaff=( [one]=a [*]=12 )
# TEST - keys and values containing spaces
unset wheat
declare -A wheat
wheat=([six]=6 [foo bar]="qux qix" )
declare -p wheat
unset wheat
declare -A wheat=([six]=6 [foo bar]="qux qix" )
recho ${wheat[foo bar]}
recho "${wheat[foo bar]}"
declare -p wheat
# TEST - basic expansions: number of elements and value length
unset wheat
typeset -A wheat
wheat=([six]=6 [foo bar]="qux qix" )
recho ${#wheat[@]}
recho ${#wheat[foo bar]}
# TEST - appending assignment operator
unset wheat
typeset -A wheat
wheat=([six]=6 [foo bar]="qux qix" )
wheat[foo bar]+=' blat'
recho ${wheat[foo bar]}
recho "${wheat[foo bar]}"
unset wheat
flix=9
typeset -Ai wheat
wheat=([six]=6 [foo bar]=flix )
wheat[foo bar]+=7
recho ${wheat[foo bar]}
recho "${wheat[foo bar]}"
unset flix wheat
# TEST - index expansion: no word splitting or globbing
typeset -A wheat
cd /tmp
touch '[sfiri]'
wheat=([s*]=6 [foo bar]=flix )
recho ${wheat[@]}
rm '[sfiri]'
cd $OLDPWD
# TEST -- associative array keys expansion
unset wheat
typeset -A wheat
wheat=([six]=6 [foo bar]=flix )
recho ${!wheat[@]}
recho "${!wheat[@]}"
# TEST -- associative array pattern removal
unset xpath
typeset -A xpath
xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )
echo ${#xpath[@]}
echo ${xpath[@]}
echo ${xpath[@]##*/}
echo ${xpath[0]##*/}
echo ${xpath[@]%%[!/]*}
echo ${xpath[0]%%[!/]*}
recho ${xpath##*/}
recho ${xpath%%[!/]*}
recho ${xpath[five]##*/}
recho ${xpath[five]%%[!/]*}
echo ${#xpath[*]}
echo ${xpath[*]}
echo ${xpath[*]##*/}
echo ${xpath[*]%%[!/]*}
# TEST -- associative array pattern substitution
unset xpath
typeset -A xpath
xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )
echo ${#xpath[@]}
# default element is "0" (as a string)
echo ${#xpath} -- ${xpath["0"]}
echo ${xpath[@]//\//^}
echo "${xpath[@]//\//^}" | cat -v
zecho "${xpath[@]/\//\\}"
zecho "${xpath[@]//\//\\}"
zecho "${xpath[@]//[\/]/\\}"
${THIS_SH} ./assoc1.sub
${THIS_SH} ./assoc2.sub
+16
View File
@@ -0,0 +1,16 @@
hash -r
echo ${BASH_CMDS[@]}
hash -p /usr/sbin/foo foo
hash -p /usr/local/bin/qux qux
echo ${!BASH_CMDS[@]}
echo ${BASH_CMDS[@]}
BASH_CMDS[blat]=/sbin/blat
BASH_CMDS[sh]=/bin/sh
hash
echo ${!BASH_CMDS[@]}
echo "${BASH_CMDS[@]}"
+15
View File
@@ -0,0 +1,15 @@
echo ${BASH_ALIASES[@]}
alias foo=/usr/sbin/foo
alias qux='/usr/local/bin/qux -l'
echo ${!BASH_ALIASES[@]}
recho ${BASH_ALIASES[@]}
BASH_ALIASES[blat]='cd /blat ; echo $PWD'
BASH_ALIASES[sh]='/bin/bash --login -o posix'
alias -p
echo ${!BASH_ALIASES[@]}
recho "${BASH_ALIASES[@]}"
+4
View File
@@ -0,0 +1,4 @@
echo "warning: all of these tests will fail if arrays have not" >&2
echo "warning: been compiled into the shell" >&2
${THIS_SH} ./assoc.tests > /tmp/xx 2>&1
diff /tmp/xx assoc.right && rm -f /tmp/xx
+6
View File
@@ -0,0 +1,6 @@
echo "warning: all of these tests will fail if arrays have not" >&2
echo "warning: been compiled into the shell" >&2
echo "warning: the BASH_ARGC and BASH_ARGV tests will fail if debugging support" >&2
echo "warning: has not been compiled into the shell" >&2
${THIS_SH} ./array.tests > /tmp/xx 2>&1
diff /tmp/xx array.right && rm -f /tmp/xx