linux - 动态评估字符串中的变量值

标签 linux bash shell scripting sh

我们需要获取动态构造变量的值。

我的意思是我们从名为 data8967677878788node 的属性文件中加载了一个变量。所以当我们运行 echo $data8967677878788node 时,我们得到输出 test

现在 data8967677878788node 中的数字部分 8967677878788 需要是动态的。这意味着可能有像

这样的变量
data1234node
data346346367node

等等。

数字是脚本的输入参数。所以我们需要这样的东西来工作

TESTVAR="data`echo $DATANUMBER`node"
echo $$TESTVAR #This line gives the value "test"

关于如何实现的任何想法

最佳答案

您可以使用 BASH 的间接变量扩展:

data346346367node='test'

myfunc() {
    datanumber="$1"
    var1="data${datanumber}node"
    echo "${!var1}"
}

并将其命名为:

myfunc 346346367

输出:

test

关于linux - 动态评估字符串中的变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37008228/

相关文章:

c++ - 在 Ubuntu 上使用 OpenGL 显示闪烁

linux - 如何添加 shell 脚本中包含的标志?

linux - 如何从shell脚本重新加载/etc/environment

mysql - 加载 INFILE 而不是 mysqlimport

bash - 使用 sed 处理 html 数据

linux - 通过 ssh 使用 git 不会获取私钥

linux - 如何避免-bash : command not found error

linux - 为什么我们需要 linux 中的 swapper 任务?

c - Linux编程: which device a file is in

linux - 获取收到的UDP数据包的目的地址