加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如果变量名称存储为字符串如何获取变量值?

发布时间:2020-12-15 16:20:08 所属栏目:安全 来源:网络整理
导读:如果我有变量名称为字符串,如何获取一个bash变量值? var1="this is the real value"a="var1"Do something to get value of var1 just using variable a. 上下文: 我有一些AMI的(Amazon Machine Image),我想启动每个AMI的几个实例。一旦他们完成引导,我
如果我有变量名称为字符串,如何获取一个bash变量值?
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中,我把一小块代码取回启动脚本并执行它。这是我在AMIs。然后每个AMI访问一个通用的配置脚本,它适用于所有AMI和每个AMI的特殊设置脚本。这些脚本是专用的,需要签名的URL才能访问它们。

所以现在,当我触发一个AMI实例(my_private_ami_1)时,我传递一个签名的URL,在S3上显示的另一个文件包含签名的URL的所有私人脚本的键/值对。

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

当启动脚本运行时,它下载上述文件和源代码。然后检查其AMI类型并为其自身选择正确的安装脚本。

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类型,实例可以照顾自己。

您可以使用$ {!a}。

这是间接parameter expansion的示例:

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

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读