9.5. 变量间接引用
优质
小牛编辑
126浏览
2023-12-01
假设一个变量的值是第二个变量的名字. 这样要如何才能从第一个变量处重新获得第二个变量的值?例如,a=letter_of_alphabet和letter_of_alphabet=z, 是否能由a引用得到z? 这确实可以办到,这种技术被称为间接引用.它使用不平常的eval var1=\$$var2序列.
例子 9-22. 间接引用
1 #!/bin/bash 2 # ind-ref.sh: 间接变量引用. 3 # 访问一个变量内容所指的变量的值. 4 5 a=letter_of_alphabet # 变量"a"保存着另外一个变量的名字. 6 letter_of_alphabet=z 7 8 echo 9 10 # 直接引用. 11 echo "a = $a" # a = letter_of_alphabet 12 13 # 间接引用. 14 eval a=\$a 15 echo "Now a = $a" # 现在 a = z 16 17 echo 18 19 20 # 现在,让我们试试更改第二次引用的顺序Now, let's try changing the second-order reference. 21 22 t=table_cell_3 23 table_cell_3=24 24 echo "\"table_cell_3\" = $table_cell_3" # "table_cell_3" = 24 25 echo -n "dereferenced \"t\" = "; eval echo \$t # 显示:dereferenced "t" = 24 26 # 在这个简单的情况下,下面的也可以工作吗?(为什么?). 27 # eval t=\$t; echo "\"t\" = $t" 28 29 echo 30 31 t=table_cell_3 32 NEW_VAL=387 33 table_cell_3=$NEW_VAL 34 echo "Changing value of \"table_cell_3\" to $NEW_VAL." 35 echo "\"table_cell_3\" now $table_cell_3" 36 echo -n "dereferenced \"t\" now "; eval echo \$t 37 # "eval"带着两个参数:"echo"和"\$t"(相当于 $table_cell_3) 38 39 echo 40 41 # (多谢Stephane Chazelas解释了上面的语句的现象) 42 43 44 # 另外一个办法是使用${!t}符号,这个在"Bash, 版本 2"章节中讨论. 45 # 参考脚本 ex78.sh. 46 47 exit 0
变量间接引用的实际用处是什么? 它提供了Bash具有C中一点指针的功能,例如,在表格查找中的用处,另外它也有其他一些有趣的应用. . . .
Nils Radtke展示了如何建立动态变量名和求它们的值。当source配置文件时这个技巧很有用。
1 #!/bin/bash 2 3 4 # --------------------------------------------- 5 # 这个文件可被另外单独的文件用source命令执行. 6 isdnMyProviderRemoteNet=172.16.0.100 7 isdnYourProviderRemoteNet=10.0.0.10 8 isdnOnlineService="MyProvider" 9 # --------------------------------------------- 10 11 12 remoteNet=$(eval "echo \$(echo isdn${isdnOnlineService}RemoteNet)") 13 remoteNet=$(eval "echo \$(echo isdnMyProviderRemoteNet)") 14 remoteNet=$(eval "echo \$isdnMyProviderRemoteNet") 15 remoteNet=$(eval "echo $isdnMyProviderRemoteNet") 16 17 echo "$remoteNet" # 172.16.0.100 18 19 # ================================================================ 20 21 # 可以做的更好. 22 23 # 注意下面的片断给出了变量getSparc, 24 #+ 但没有变量getIa64: 25 26 chkMirrorArchs () { 27 arch="$1"; 28 if [ "$(eval "echo \${$(echo get$(echo -ne $arch | 29 sed 's/^\(.\).*/\1/g' | tr 'a-z' 'A-Z'; echo $arch | 30 sed 's/^.\(.*\)/\1/g')):-false}")" = true ] 31 then 32 return 0; 33 else 34 return 1; 35 fi; 36 } 37 38 getSparc="true" 39 unset getIa64 40 chkMirrorArchs sparc 41 echo $? # 0 42 # 真 43 44 chkMirrorArchs Ia64 45 echo $? # 1 46 # 假 47 48 # 注意: 49 # ----- 50 # Even the to-be-substituted variable name part is built explicitly. 51 # The parameters to the chkMirrorArchs calls are all lower case. 52 # The variable name is composed of two parts: "get" and "Sparc" . . .
例子 9-23. 传递一个间接引用给awk
1 #!/bin/bash 2 3 # Another version of the "column totaler" script 4 #+ that adds up a specified column (of numbers) in the target file. 5 # This one uses indirect references. 6 7 ARGS=2 8 E_WRONGARGS=65 9 10 if [ $# -ne "$ARGS" ] # Check for proper no. of command line args. 11 then 12 echo "Usage: `basename $0` filename column-number" 13 exit $E_WRONGARGS 14 fi 15 16 filename=$1 17 column_number=$2 18 19 #===== Same as original script, up to this point =====# 20 21 22 # A multi-line awk script is invoked by awk ' ..... ' 23 24 25 # Begin awk script. 26 # ------------------------------------------------ 27 awk " 28 29 { total += \${column_number} # indirect reference 30 } 31 END { 32 print total 33 } 34 35 " "$filename" 36 # ------------------------------------------------ 37 # End awk script. 38 39 # Indirect variable reference avoids the hassles 40 #+ of referencing a shell variable within the embedded awk script. 41 # Thanks, Stephane Chazelas. 42 43 44 exit 0
间接引用的方法是个小窍门。如果第二个变量更改了它的值,第一个变量必须适当地解除引用(就像上面的例子一样)This method of indirect referencingis a bit tricky. If the second order variable changes its value, thenthe first order variable must be properly dereferenced (as in theabove example).幸运地是在bash(参考例子。。。)版本2中介绍的${!variable}符号能使间接引用更智能一些。 notation introduced with version 2of Bash (see Example 34-2) makes indirect referencing more intuitive.
Bash不支持指针运算,这极大地限制了间接引用的用处。事实上,在脚本语言里间接引用是一个蹩脚地东西。