如果变量名被存储为字符串如何获得一个变量的值? [英] How to get a variable value if variable name is stored as string?

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

问题描述

我如何可以检索一个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机器映像),我要火了每个AMI的几个实例。当他们完成引导过程,我想设置每个实例根据其AMI类型。我不想烤手的AMI内部脚本或密钥的,所以我prepared一个广义的启动脚本,我把它放在 S3与可公开访问的链接。在rc.local中我把小块code这取的启动脚本并执行它。这是我在阿美族。然后每个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)的一个实例,我通过了psented S3的多个文件$ P $其中包含签署网址为所有的私人脚本键/值对的方面签署的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"
...


当启动脚本运行时,它会下载上面的文件和 源的它。然后检查其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

所以,现在我可以有一个通用的code能发射情况下,不论其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}

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

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

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

如果参数的第一个字符是一个感叹号(!),它
  引入变量间接的水平。 Bash使用的价值
  从参数的其余部分形成的可变作为的名字
  变量;然后这个变量被扩展并且该值在使用
  替代,而不是参数本身。价值的其余部分。

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天全站免登陆