简体   繁体   English

如何使用bash中的字符串值作为变量名

[英]How to use the value of a string in bash as a variable name

Hopefully I can make it clear. 希望我能说清楚。 I would like to create a filename out of different strings in bash . 我想从bash的不同字符串创建文件名。 For example hmd.sh so h , m , d are different values (number 0..9 or letter aA..zZ ). 例如hmd.sh so hmd是不同的值(数字0..9或字母aA..zZ )。 So for example I want to convert 所以例如我想转换

h=1 m=11 and d=12 to 1aA.sh. h=> 1, m=>a and d=>A 

To declare variables like 像这样声明变量

a01=1; a02=2 .. a09=9, a10=0; a11=a; a12=b and so on. h(1)=a01=1 m(11)=a11=a 

and

d(12)=a12=A.

To test it I wrote this: 为了测试它,我这样写:

#!/bin/bash
dd01="1"
aa="01"
bb="dd$aa"
echo $bb

But of course $bb is dd01 and not its value. 但是当然$bbdd01而不是它的值。 How can I make $bb its value of 1? 如何使$bb的值为1?

Associative arrays make this kind of thing much more readable. 关联数组使这种事情更具可读性。

However your answer is "variable indirection" 但是,您的答案是“可变间接”

$ echo $bb
dd01
$ echo ${!bb}
1

Do not listen to any advice suggesting eval -- you open yourself up to all kinds of code injection. 不要听任何建议eval建议-您可以接受各种代码注入。

The only way to expand a variable inside another is in an array when the variable is enclosed in the key's brackets like [$var] . 将变量扩展到另一个变量内的唯一方法是将变量括在[$var]之类的键括号内。

You could store your values in an associative array, and reference them like so: 您可以将值存储在关联数组中,并像这样引用它们:

declare -A arr
arr[dd01]="1"
arr[aa]="01"
arr[bb]="dd${arr[aa]}"
echo ${arr[${arr[bb]}]}

Using arrays like this may be more convoluted for this example than referencing the variable name using ${!bb} syntax, but if you need to do this while keeping different sets of variables that may need to reference each other, creating an associative array may make more organizational sense. 与使用${!bb}语法引用变量名称相比,在本示例中使用这样的数组可能会更费时,但是如果您需要在保留可能需要互相引用的不同变量集的同时进行此操作,则可以创建一个关联数组更具组织意义。

I rewrote your code example as follows which gives you the value of 1 which is what you are trying to achieve. 我重新编写了以下代码示例,该示例为您提供了1的值,这是您要实现的目标。

#!/bin/bash
dd01="1"
aa="01"
bb="dd$aa"
echo $[$bb]

This did the trick: 这达到了目的:

#!/bin/bash
dd=1234567890aAbBcC
aa="11"
echo ${dd:(aa-1):1}

Appearantly 1 is the 0 position and aa can also be like 01 and still work! 显然1是0位置,aa也可以像01一样继续工作!

Thank for all the advices. 感谢您的所有建议。

I found my answer here: 我在这里找到了答案:

http://tldp.org/LDP/abs/html/string-manipulation.html http://tldp.org/LDP/abs/html/string-manipulation.html

This should do the job: 这应该可以完成以下工作:

#!/bin/bash
dd01="1"
aa="01"
bb="dd$aa"
eval echo \$$bb

You can use the '$' operator to access the value of a variable. 您可以使用“ $”运算符来访问变量的值。 For example... 例如...

d = 'Hi'
e = ' there '
f = 'friend.'
foo = '$d$e$f'

This would cause the value of foo to be 'Hi there friend.' 这将导致foo的值成为“ Hi在那里的朋友”。

Hope this helps. 希望这可以帮助。

Use eval : 使用eval

#!/bin/bash
dd01="1"
aa="01"
eval bb=\$dd$aa
echo $bb

This script outputs the expected 1 . 该脚本输出预期的1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM