📄 assoc.tests
字号:
# TEST - basic declaration and assignmenttypeset -A fluffdeclare -Afluff[foo]=onefluff[bar]=twodeclare -Adeclare -p fluffunset fluff[foo]declare -p flufffluff[bar]=newvaldeclare fluff[qux]=assigneddeclare -p fluffunset fluff# TEST - compount assignment and variable attributesdeclare -A wheat chaffwheat=( [zero]=0 [one]=a [two]=b [three]=c )declare -i chaffchaff=( [zero]=1+4 [one]=3+7 four )declare -A waste=( [pid]=42134 [version]=4.0-devel [source]=$0 [lineno]=$LINENO )declare -r wastedeclare -Adeclare +i chaffchaff[hello world]=flipdeclare -p chaff# TEST - errorsunset wastechaff[*]=12chaff=( [one]=a [*]=12 )# TEST - key expansion -- no word splittingchaff[hello world]=flipdeclare -p chaffecho ${chaff[hello world]}chaff[box]="multiple words"recho ${chaff[@]}recho "${chaff[@]}"recho ${chaff[*]}recho "${chaff[*]}"unset chaffdeclare -A chaff[200]declare +A chaffchaff[*]=12chaff=( [one]=a [*]=12 )# TEST - keys and values containing spacesunset wheatdeclare -A wheatwheat=([six]=6 [foo bar]="qux qix" )declare -p wheatunset wheatdeclare -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 lengthunset wheattypeset -A wheatwheat=([six]=6 [foo bar]="qux qix" )recho ${#wheat[@]}recho ${#wheat[foo bar]}# TEST - appending assignment operatorunset wheattypeset -A wheatwheat=([six]=6 [foo bar]="qux qix" )wheat[foo bar]+=' blat'recho ${wheat[foo bar]}recho "${wheat[foo bar]}"unset wheatflix=9typeset -Ai wheatwheat=([six]=6 [foo bar]=flix )wheat[foo bar]+=7recho ${wheat[foo bar]}recho "${wheat[foo bar]}"unset flix wheat# TEST - index expansion: no word splitting or globbingtypeset -A wheatcd /tmptouch '[sfiri]'wheat=([s*]=6 [foo bar]=flix )recho ${wheat[@]}rm '[sfiri]'cd $OLDPWD# TEST -- associative array keys expansionunset wheattypeset -A wheatwheat=([six]=6 [foo bar]=flix )recho ${!wheat[@]}recho "${!wheat[@]}"# TEST -- associative array pattern removalunset xpathtypeset -A xpathxpath=( [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 substitutionunset xpathtypeset -A xpathxpath=( [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 -vzecho "${xpath[@]/\//\\}"zecho "${xpath[@]//\//\\}"zecho "${xpath[@]//[\/]/\\}"# test assignment to key "0"unset Tdeclare -A TT='([a]=1)'echo "${T[@]}"unset T${THIS_SH} ./assoc1.sub${THIS_SH} ./assoc2.sub${THIS_SH} ./assoc3.sub${THIS_SH} ./assoc4.sub${THIS_SH} ./assoc5.sub${THIS_SH} ./assoc6.sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -