变量变量 [英] variable of Variable
问题描述
我必须打印一个可变值,该可变值是art变量。
例如变量是
I have to print a variable value which is art variable. eg. variables are
A = X
Z_X = Test
使用
setenv A X
setenv Z_X Test
我想使用$ A打印$ Z_X值
I want to print $Z_X value using $A
我正在尝试,但没有成功。
I am trying but without any success.
echo ${Z_${A}}
echo ${Z_[$A]}
谁能告诉我我错了。
问候
推荐答案
A = X
Z_X =测试
A = X
Z_X = Test
这似乎是错误的;在 csh
中,您需要使用 set
关键字来分配变量;此外,约定还对普通变量使用小写字母,对环境变量使用大写字母:
This seems wrong; in csh
, you need to use the set
keyword to assign variables; in addition, the convention is also to use lower case for "normal" variables, and UPPER CASE for environment variables:
set a = x
set z_x = Test
然后可以使用 eval
得到想要的东西:
You can then use eval
to get what you want:
% eval echo \$z_$a
Test
% set x = `eval echo \$z_$a`
% echo $s
Test
如果您不相信 $ a
的来源,则此可能很危险,因为它也可能会执行 rm -rf /
或类似的危险操作(但如果您相信 $ a
,这非常好)。
This may be dangerous if you don't trust the source of $a
, since it may also do a rm -rf /
or something similarly dangerous (but if you trust the source of $a
, it's perfectly fine).
您可以使用 set $ c $来获取 all 变量的列表。 c>:
You can get a list of all variables with set
:
% set | grep ^z_$a
z_x Test
% set | grep ^z_$a | awk '{print $1}'
z_x
唯一的安全我可以确定要做什么的方法。
Which is the only safe way I can figure out to do what you want.
这篇关于变量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!