击,串联两个字符串来引用一个变量3 [英] Bash, Concatenating 2 strings to reference a 3rd variable
问题描述
我有我有串联2个变量调用3的一些问题bash脚本。
I have a bash script I am having some issues with concatenating 2 variables to call a 3rd.
下面是脚本的简化,但语法是阅读文档后躲避我。
Here is a simplification of the script, but the syntax is eluding me after reading the docs.
server_list_all="server1 server2 server3";
var1 = "server";
var2 = "all";
echo $(($var1_list_$var2));
这是最接近我得到正确的答案,它承认字符串和符号化扔一个错误。
This is about as close as I get to the right answer, it acknowledges the string and tosses an error on tokenization.
syntax error in expression (error token is "server1 server2 server3....
没有真正看到在文档的这样的东西,但它应该是可行的。
Not really seeing anything in the docs for this, but it should be doable.
编辑:清理了一下
推荐答案
借助 Bash的参考手册介绍如何使用参数扩展的一个很棒的功能做一些间接。在你的情况,你有兴趣在寻找一个变量,其名称由其他两个变量定义的内容:
The Bash Reference Manual explains how you can use a neat feature of parameter expansion to do some indirection. In your case, you're interested in finding the contents of a variable whose name is defined by two other variables:
server_list_all="server1 server2 server3"
var1=server
var2=all
combined=${var1}_list_${var2}
echo ${!combined}
指当感叹号组合
表示使用其名称由内容定义的变量组合
The exclamation mark when referring to combined
means "use the variable whose name is defined by the contents of combined
"
这篇关于击,串联两个字符串来引用一个变量3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!