Is there a way in ksh to get a variable's value when you have been given the name of the variable?
当你被赋予变量的名称时,ksh中有没有办法获得变量的值?
For example:
#!/usr/bin/ksh
var_name=$1 #pretend here that the user passed the string "PATH"
echo ${$var_name} #echo value of $PATH -- what do I do here?
4 个解决方案
#1
var_name=$1 #pretend here that the user passed the string "PATH"
printenv $var_name
#2
eval `echo '$'$var_name`
echo concatenates a '$' to the variable name inside $var_name, eval evaluates it to show the value.
echo将'$'连接到$ var_name中的变量名称,eval会对其进行评估以显示该值。
EDIT: The above isn't quite right. The correct answer is with no backticks.
编辑:以上不太正确。正确答案是没有反引号。
eval echo '$'$var_name
#3
printenv
is not a ksh builtin and may not always be present. For older ksh versions, prior to ksh93, the eval 'expression' method works best.
printenv不是一个内置的ksh,可能并不总是存在。对于较旧的ksh版本,在ksh93之前,eval'expression'方法效果最好。
A powerful method in ksh93 is to use indirection variables with 'nameref' or 'typeset -n'.
ksh93中一个强大的方法是使用带有'nameref'或'typeset -n'的间接变量。
Define and verify a nameref variable that refers to $PATH
:
定义并验证引用$ PATH的nameref变量:
$ nameref indirect=PATH
$ print $indirect
/usr/bin:/usr/sbin
See how the nameref variable changes when we change PATH
:
在我们更改PATH时,查看nameref变量如何更改:
$ PATH=/usr/bin:/usr/sbin:/usr/local/bin
$ print $indirect
/usr/bin:/usr/sbin:/usr/local/bin
Show ksh version and the alias for nameref
:
显示ksh版本和nameref的别名:
$ type nameref
nameref is an alias for 'typeset -n'
$ echo ${.sh.version}
Version JM 93t+ 2010-02-02
#4
For one step above your answer (I spent a lot of time trying to find both these answers). The below will allow you to export a dynamic variable and then recall it dynamically:
比你的答案高出一步(我花了很多时间试图找到这两个答案)。下面将允许您导出动态变量,然后动态调用它:
echo -n "Please provide short name for path:"
read PATH_SHORTCUT
echo -n "Please provide path:"
read PATH
eval export \${PATH_SHORTCUT}_PATH="${PATH}"
eval echo Path shortcut: ${PATH_SHORTCUT} set to \$"${PATH_SHORTCUT}_PATH".
#1
var_name=$1 #pretend here that the user passed the string "PATH"
printenv $var_name
#2
eval `echo '$'$var_name`
echo concatenates a '$' to the variable name inside $var_name, eval evaluates it to show the value.
echo将'$'连接到$ var_name中的变量名称,eval会对其进行评估以显示该值。
EDIT: The above isn't quite right. The correct answer is with no backticks.
编辑:以上不太正确。正确答案是没有反引号。
eval echo '$'$var_name
#3
printenv
is not a ksh builtin and may not always be present. For older ksh versions, prior to ksh93, the eval 'expression' method works best.
printenv不是一个内置的ksh,可能并不总是存在。对于较旧的ksh版本,在ksh93之前,eval'expression'方法效果最好。
A powerful method in ksh93 is to use indirection variables with 'nameref' or 'typeset -n'.
ksh93中一个强大的方法是使用带有'nameref'或'typeset -n'的间接变量。
Define and verify a nameref variable that refers to $PATH
:
定义并验证引用$ PATH的nameref变量:
$ nameref indirect=PATH
$ print $indirect
/usr/bin:/usr/sbin
See how the nameref variable changes when we change PATH
:
在我们更改PATH时,查看nameref变量如何更改:
$ PATH=/usr/bin:/usr/sbin:/usr/local/bin
$ print $indirect
/usr/bin:/usr/sbin:/usr/local/bin
Show ksh version and the alias for nameref
:
显示ksh版本和nameref的别名:
$ type nameref
nameref is an alias for 'typeset -n'
$ echo ${.sh.version}
Version JM 93t+ 2010-02-02
#4
For one step above your answer (I spent a lot of time trying to find both these answers). The below will allow you to export a dynamic variable and then recall it dynamically:
比你的答案高出一步(我花了很多时间试图找到这两个答案)。下面将允许您导出动态变量,然后动态调用它:
echo -n "Please provide short name for path:"
read PATH_SHORTCUT
echo -n "Please provide path:"
read PATH
eval export \${PATH_SHORTCUT}_PATH="${PATH}"
eval echo Path shortcut: ${PATH_SHORTCUT} set to \$"${PATH_SHORTCUT}_PATH".