3
A
回答
6
在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
的代碼整個片斷從是s ITE:
#!/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)
相關問題
- 1. 是否有可能引用變量
- 2. 是否有可能在變量上調用jquery函數?
- 3. 變量打壞
- 4. 是否有可能破壞上下文?
- 5. 是否有可能從變量
- 6. PHP變量變量是否可能。 $$$ a
- 7. KSH:在KornShell(KSH)與可變
- 8. 是否有可能在變量中更改變量的值?
- 9. 是否有可能獲得變量的最大可能長度
- 10. 是否可以打印變量名稱?
- 11. 是否有可能從android調用WSHTTPBINDING?
- 12. 檢查一個變量是否已設置,值是ksh
- 13. 是否有可能破壞OCaml中的處理異常?
- 14. 是否有可能從Java的超類中調用子類的實例變量?
- 15. KSH中的變量範圍
- 16. 是否有用於Windows的KornShell(ksh)IDE?
- 17. 變量的List <>是否可能?
- 18. 是否有可能將SQLiteDatabase變成全局變量?
- 19. 是否有可能使.gitignore根據環境變量而改變
- 20. 是否有可能使用一個變量的javascript參考
- 21. 是否有可能用定時器刪除會話變量?
- 22. 是否有可能使用變量嵌套命令splatting
- 23. 是否有可能用變量替換部分字符串?
- 24. 是否有可能爲客戶使用變量?
- 25. 是否有可能在python中使用變量作爲代碼
- 26. 是否有可能在PHP中使用JavaScript的變量?
- 27. 是否有可能在bash中使用變量作爲語法?
- 28. 是否有可能「過度使用」會話變量?
- 29. 是否有可能在IF中使用全局變量?
- 30. 是否有可能使用javascript更新jinja2模板變量?
你肯定安全這個'eval'使用? –