在bash中创建一个关联数组

3
在这个bash脚本中,我试图将两个数组合并成一个关联数组,使得在combineArray中,newFSarray中的第0项为键,而oldFSarray中的第0项则是相应的值,以此类推,共133项。
因为我对BASH不太熟悉,所以我完全按照自己的方式粘贴了整个代码。很可能我会犯语法错误。目前我认为for loop中有些问题,因为在输出中,无论我选择什么key,我只能得到最后一个值(请参见下面的当前输出)。
#! /bin/bash

declare -i arrayNum=$1-1

declare -a oldFSarray=(prollecture1 prollecture2 prollecture3 prollecture4 prollecture4b prollecture5 prollecture6 prollecture7 prollecture8 prollecture9 prollecture10 prollecture11 prollecture12 prollecture13 prollecture14 prollecture15 prollecture16 prollecture17 prollecture18 dthreelecture19 dthreeaonelecture20 dthreeaonelecture21 dthreeaonelecture22 dthreeaonelecture23 dthreeaonelecture24 dthreeatwolecture25 dthreeathreelecture26 dthreeafourlecture27 dthreeafivelecture28 dthreeafivelecture29 dthreeasixlecture30 dthreesixlecture30b dthreeasixlecture31 donelecture32 donelecture33 donelecture34 donelecture35 donelecture36 donelecture37 donelecture38 donelecture39 donelecture40 donelecture41 donelecture42 donelecture43 donelecture44 donelecture45 donelecture46 donelecture47 donelecture48 donelecture49 donelecture50 donelecture51 donelecture52 donelecture53 dTlecture54 dTlecture55 dTlecture56 dTlecture57 dTlecture58 dTlecture59 dTlecture60 dTlecture61 dTlecture62 dTlecture63 dTlecture64 dTlecture65 dTlecture66 dTlecture67 dTlecture68 dTlecture69 dTlecture70 dTlecture71 dTlecture72 dTlecture73 dTlecture74 dTlecture75 dTlecture76 dTlecture77 dClecture78 dClecture79 dClecture80 dClecture81 dClecture82 dClecture83 dClecture84 dClecture85 dClecture86 dClecture87 dClecture88 dClecture89 dClecture90 dClecture91 dClecture92 dLlecture93 dLlecture94 dLlecture95 dLlecture96 dLlecture97 dLlecture98 dLlecture99 dLlecture100 dLlecture101 dLlecture102 dLlecture103 dLlecture104 dLlecture105 dLlecture106 dLlecture107 dLlecture108 dLlecture109 dLlecture110 dLlecture111 dLlecture112 dLlecture113 dLlecture114 dLlecture115 dLlecture116 dLlecture117 dLlecture118 dLlecture119 dLlecture120 dLlecture121 dLlecture122 dLlecture123 dLlecture124 dLlecture125 dLlecture126 dLlecture127 dIlecture128 dIlecture129 dIlecture130 dIlecture131 dIlecture132 )

declare -a newFSarray=(Lectio_1 Lectio_2 Lectio_3 Lectio_4 Lectio_5 Lectio_6 Lectio_7 Lectio_8 Lectio_9 Lectio_10 Lectio_11 Lectio_12 Lectio_13 Lectio_14 Lectio_15 Lectio_16 Lectio_17 Lectio_18 Lectio_19 Lectio_20 Lectio_21 Lectio_22 Lectio_23 Lectio_24 Lectio_25 Lectio_26 Lectio_27 Lectio_28 Lectio_29 Lectio_30 Lectio_31 Lectio_32 Lectio_33 Lectio_34 Lectio_35 Lectio_36 Lectio_37 Lectio_38 Lectio_39 Lectio_40 Lectio_41 Lectio_42 Lectio_43 Lectio_44 Lectio_45 Lectio_46 Lectio_47 Lectio_48 Lectio_49 Lectio_50 Lectio_51 Lectio_52 Lectio_53 Lectio_54 Lectio_55 Lectio_56 Lectio_57 Lectio_58 Lectio_59 Lectio_60 Lectio_61 Lectio_62 Lectio_63 Lectio_64 Lectio_65 Lectio_66 Lectio_67 Lectio_68 Lectio_69 Lectio_70 Lectio_71 Lectio_72 Lectio_73 Lectio_74 Lectio_75 Lectio_76 Lectio_77 Lectio_78 Lectio_79 Lectio_80 Lectio_81 Lectio_82 Lectio_83 Lectio_84 Lectio_85 Lectio_86 Lectio_87 Lectio_88 Lectio_89 Lectio_90 Lectio_91 Lectio_92 Lectio_93 Lectio_94 Lectio_95 Lectio_96 Lectio_97 Lectio_98 Lectio_99 Lectio_100 Lectio_101 Lectio_102 Lectio_103 Lectio_104 Lectio_105 Lectio_106 Lectio_107 Lectio_108 Lectio_109 Lectio_110 Lectio_111 Lectio_112 Lectio_113 Lectio_114 Lectio_115 Lectio_116 Lectio_117 Lectio_118 Lectio_119 Lectio_120 Lectio_121 Lectio_122 Lectio_123 Lectio_124 Lectio_125 Lectio_126 Lectio_127 Lectio_128 Lectio_129 Lectio_130 Lectio_131 Lectio_132 Lectio_133 Lectio 134)

declare -a combineArray=();

for i in {0..133}
    do
        combineArray=("${combineArray[@]}" [$newFSarray[$i]]="${oldFSarray[$i]}");
    done

echo "old: "
echo ${oldFSarray[$arrayNum]}
new=${newFSarray[$arrayNum]}
echo "new: "
echo $new
echo ${combineArray[$new]}

这是当前的输出结果。因为我将2作为初始参数传递,我期望得到 prollecture2,但实际上得到了 dIlecture132(可能的最后一个值)。

$ ./plaoulFSArray.sh 2
old: 
prollecture2
new: 
Lectio_2
dIlecture132

(顺便问一下,Bash 有类似于 PHP 的 print_r 函数吗?这样我就可以看到 combineArray 的结果了吗?) - Jeff
2
请跟我重复:Bash 不适合处理任何复杂的事情。Bash 不适合处理任何复杂的事情。五分钟后,现在去用一种适合编写脚本的语言来写你的脚本,而不是用 Bash。;-) - Abhi Beckert
1个回答

5
你的 for 循环应该长这样:
declare -A combineArray
for ((i=0; i<${#oldFSarray[@]}; i++))
    do
        combineArray+=(["${newFSarray[i]}"]="${oldFSarray[i]}")
    done

即使是关联数组,"${array[@]}"仅返回值("${!array[@]}"返回键)。使用+=(...)来追加到数组中。
像这样倾倒一个数组:declare -p combineArray

奇怪 - 我没有添加它们 - 我的机器似乎正在为我添加它们 :( - Jeff
1
我认为 /bin/bash 不是 4.2 版本,因为苹果仍然使用版本 3(不支持关联数组)作为系统 shell。你可能正在使用更新的已安装版本用于登录 shell,但你的脚本正在使用原始版本。 - chepner
这太麻烦了 - 我能用macports更新/bin/bash吗? - Jeff
@glenn jackman,我接受了你的答案,因为它似乎是一个很好的解决方案。听起来我的新问题是升级bash(感谢@chepner)。这是一个不同讨论的新问题 :) - Jeff
2
我不建议替换 /bin/bash;在新版本中,bash 并不保持完全的向后兼容性(可以看到各种可用的 compat* 选项)。相反,将脚本中的哈希铭文更改为 !#/usr/local/bin/bash 或您安装的较新版本的 bash 所在位置。 - chepner
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接