【问题标题】:Is it possible to bash/ksh call a variable of variable是否可以 bash/ksh 调用变量的变量
【发布时间】:2011-11-11 14:48:33
【问题描述】:

是否可以bash/ksh调用一个变量的变量,例如:

set -A MY_ARRAY ${${var}_something}

BR 科勒萨

【问题讨论】:

    标签: bash ksh var


    【解决方案1】:

    在 bash 中:

    VAR1=aap
    VAR2=noot
    
    USEVARNAME=VAR2
    echo ${!USEVARNAME}
    
    USEVARNAME=VAR1
    echo ${!USEVARNAME}
    

    打印

    noot
    aap
    

    【讨论】:

      【解决方案2】:

      您可能指的是间接引用:http://tldp.org/LDP/abs/html/ivr.html

      # Indirect reference.
      eval a=\$$a
      

      来自该站点的整个 sn-p 代码:

      #!/bin/bash
      # ind-ref.sh: Indirect variable referencing.
      # Accessing the contents of the contents of a variable.
      
      # First, let's fool around a little.
      
      var=23
      
      echo "\$var   = $var"           # $var   = 23
      # So far, everything as expected. But ...
      
      echo "\$\$var  = $$var"         # $$var  = 4570var
      #  Not useful ...
      #  \$\$ expanded to PID of the script
      #  -- refer to the entry on the $$ variable --
      #+ and "var" is echoed as plain text.
      #  (Thank you, Jakob Bohm, for pointing this out.)
      
      echo "\\\$\$var = \$$var"       # \$$var = $23
      #  As expected. The first $ is escaped and pasted on to
      #+ the value of var ($var = 23 ).
      #  Meaningful, but still not useful. 
      
      # Now, let's start over and do it the right way.
      
      # ============================================== #
      
      
      a=letter_of_alphabet   # Variable "a" holds the name of another variable.
      letter_of_alphabet=z
      
      echo
      
      # Direct reference.
      echo "a = $a"          # a = letter_of_alphabet
      
      # Indirect reference.
        eval a=\$$a
      # ^^^        Forcing an eval(uation), and ...
      #        ^   Escaping the first $ ...
      # ------------------------------------------------------------------------
      # The 'eval' forces an update of $a, sets it to the updated value of \$$a.
      # So, we see why 'eval' so often shows up in indirect reference notation.
      # ------------------------------------------------------------------------
        echo "Now a = $a"    # Now a = z
      
      echo
      
      # Now, let's try changing the second-order reference.
      
      t=table_cell_3
      table_cell_3=24
      echo "\"table_cell_3\" = $table_cell_3"            # "table_cell_3" = 24
      echo -n "dereferenced \"t\" = "; eval echo \$$t    # dereferenced "t" = 24
      # In this simple case, the following also works (why?).
      #         eval t=\$$t; echo "\"t\" = $t"
      
      echo
      
      t=table_cell_3
      NEW_VAL=387
      table_cell_3=$NEW_VAL
      echo "Changing value of \"table_cell_3\" to $NEW_VAL."
      echo "\"table_cell_3\" now $table_cell_3"
      echo -n "dereferenced \"t\" now "; eval echo \$$t
      # "eval" takes the two arguments "echo" and "\$$t" (set equal to $table_cell_3)
      

      【讨论】:

      • 你确定这个eval使用的安全吗?
      猜你喜欢
      • 2016-03-30
      • 2020-02-16
      • 2023-03-25
      • 2011-04-27
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 2020-04-04
      相关资源
      最近更新 更多