从另一个字符串的值创建一个字符串变量名 [英] Creating a string variable name from the value of another string

查看:113
本文介绍了从另一个字符串的值创建一个字符串变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的bash脚本我有两个变量 CONFIG_OPTION CONFIG_VALUE 包含字符串 VENDOR_NAME Default_Vendor 分别。

In my bash script I have two variables CONFIG_OPTION and CONFIG_VALUE which contain string VENDOR_NAME and Default_Vendor respectively.

我需要创建一个名称的变量 $ CONFIG_OPTION VENDOR_NAME 和<$ C $赋值C> CONFIG_VALUE 来新创建的变量。

I need to create a variable with name $CONFIG_OPTION ie VENDOR_NAME and assign the value in CONFIG_VALUE to newly created variable.

我怎样才能做到这一点?

How I can do this?

我试过

$CONFIG_OPTION=$CONFIG_VALUE

但我正在逐渐在这条线的误差

But I am getting an error on this line as

'./Build.bash: line 137: VENDOR_NAME="Default_Vendor": command not found'

感谢。

推荐答案

我知道,没有人会提到它,所以在这里我走了。您可以使用的printf

I know that nobody will mention it, so here I go. You can use printf!

#!/bin/bash

CONFIG_OPTION="VENDOR_NAME"
CONFIG_VALUE="Default_Vendor"

printf -v $CONFIG_OPTION "$CONFIG_VALUE"

# Don't believe me?
echo "$VENDOR_NAME"

完成!

顺便说一句,这是一个相当快的方法(一样快的声明法)。

Incidentally, it's a rather fast method (just as fast as the declare method).

这篇关于从另一个字符串的值创建一个字符串变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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