如果变量名称存储为字符串,如何获取变量值? [英] How to get a variable value if variable name is stored as string?

查看:32
本文介绍了如果变量名称存储为字符串,如何获取变量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将变量名称作为字符串,如何检索 bash 变量值?

How can I retrieve a bash variable value if I have the variable name as string?

var1="this is the real value"
a="var1"
Do something to get value of var1 just using variable a.

我有一些 AMI(Amazon Machine Image),我想启动几个实例每个 AMI.一旦他们完成启动,我想根据其 AMI 类型设置每个实例.我不想在任何 AMI 中烘焙大量脚本或密钥,所以我准备了一个通用的启动脚本并将其放在 S3 带有可公开访问的链接.在 rc.local 中,我放了一小段代码来获取启动脚本并执行它.这就是我在 AMI 中的全部内容.然后每个 AMI 访问一个通用配置脚本,该脚本适用于所有 AMI 和每个 AMI 的特殊设置脚本.这些脚本是私有的,需要经过签名的 URL 才能访问.

I have some AMI's (Amazon Machine Image) and I want to fire up a few instances of each AMI. As soon as they finish booting, I want to setup each instance according to its AMI type. I don't want to bake lots of scripts or secret keys inside any AMI so I prepared a generalized startup script and I put it on S3 with a publicly accessible link. In rc.local I put small piece of code which fetches the startup script and executes it. This is all I have in the AMIs. Then each AMI accesses a common configuration script which is applicable to all AMIs and special setup scripts for each. These scripts are private and require a signed URL to access them.

所以现在,当我启动一个 AMI 实例 (my_private_ami_1) 时,我为 S3 上显示的另一个文件传递了一个签名 URL,其中包含所有私有脚本在键/值对方面的签名 URL.

So now, when I fire an instance of an AMI (my_private_ami_1), I pass a signed URL for one more file presented on S3 which contains signed URL for all private scripts in terms of key/value pair.

config_url="http://s3.amazo.../config?signature"
my_private_ami_1="http://s3.amazo.../ami_1?signature"
...

当启动脚本运行时,它会下载上面的文件和source.然后它会检查其 AMI 类型并为自己选择正确的设置脚本.

When the startup script runs, it downloads the above file and source's it. Then it checks for its AMI type and picks the correct setup script for itself.

ami\_type=GET AMI TYPE #ex: sets ami\_type to my\_private\_ami\_1
setup\_url=GET THE SETUP FILE URL BASED ON AMI\_TYPE # this is where this problem arises

所以现在我可以有一个通用代码,它可以触发实例而不管它们的 AMI 类型如何,并且实例可以自行处理.

So now I can have a generic code which can fire instances irrespective of their AMI types and instances can take care of themselves.

推荐答案

你可以使用${!a}:

var1="this is the real value"
a="var1"
echo "${!a}" # outputs 'this is the real value'

这是一个间接参数扩展的例子:

参数扩展的基本形式是${parameter}.的价值参数被替换.

The basic form of parameter expansion is ${parameter}. The value of parameter is substituted.

如果参数的第一个字符是感叹号(!),它引入了一个可变间接级别.Bash 使用由 parameter 的其余部分组成的变量作为多变的;然后扩展此变量,并在替换的其余部分,而不是 parameter 本身的值.

If the first character of parameter is an exclamation point (!), it introduces a level of variable indirection. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself.

这篇关于如果变量名称存储为字符串,如何获取变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆