Bash 创建变量然后为其赋值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4882745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:23:29  来源:igfitidea点击:

Bash create variable then assign value to it

linuxbashscripting

提问by mike

For this problem I have two values, curdirand curlevel, which change throughout my script. I want to know if it's possible to create a variable and then use that value as the name for another value. For example

对于这个问题,我有两个值curdircurlevel,它们在我的脚本中都发生了变化。我想知道是否可以创建一个变量,然后将该值用作另一个值的名称。例如

temp="dir_${curdir}_${curlevel}"
$temp=$name_of_directory  **<----Is there a legitimate way to do this?**

so if initially curdir=1and curlevel=0then $(temp)=directory_oneis equal to

因此,如果最初curdir=1curlevel=0然后 $(temp)=directory_one是等于

dir_1_0=directory_one

then later if curdir=2and curlevel=4, I can reset temp and then have

然后稍后如果curdir=2curlevel=4,我可以重置温度然后有

$(temp)=another_directory

is the same as

是相同的

dir_2_4=another_directory

so I could make a call such as

所以我可以拨打电话,例如

cd $(temp)

which will move me to different directories when I need to

当我需要时,这会将我移动到不同的目录

采纳答案by Brian

I think what you want is to use eval. Like so:

我认为你想要的是使用eval. 像这样:

 $ foo=bar
 $ bar=baz
 $ eval qux=$$foo
 $ echo $qux
 baz

So what you could do is something like

所以你可以做的是

eval temp=$$"dir_${curdir}_${curlevel}"
cd $temp

回答by Jonathan Leffler

The trick for this is to use eval- several times.

诀窍是使用eval- 多次。

curdir=1
curlevel=0
temp='dir_${curdir}_${curlevel}'      # Note single quotes!
x=$(eval echo $temp)
eval $x=$PWD
cd /tmp
curdir=2
curlevel=4
x=$(eval echo $temp)
eval $x=$PWD

echo $dir_1_0
echo $dir_2_4

The output of sh -x script:

的输出sh -x script

+ curdir=1
+ curlevel=0
+ temp='dir_${curdir}_${curlevel}'
++ eval echo 'dir_${curdir}_${curlevel}'
+++ echo dir_1_0
+ x=dir_1_0
+ eval dir_1_0=/Users/jleffler/tmp/soq
++ dir_1_0=/Users/jleffler/tmp/soq
+ cd /tmp
+ curdir=2
+ curlevel=4
++ eval echo 'dir_${curdir}_${curlevel}'
+++ echo dir_2_4
+ x=dir_2_4
+ eval dir_2_4=/tmp
++ dir_2_4=/tmp
+ echo /Users/jleffler/tmp/soq
/Users/jleffler/tmp/soq
+ echo /tmp
/tmp

The output of sh script:

的输出sh script

/Users/jleffler/tmp/soq
/tmp


Converted to a function:

转换为函数:

change_dir()
{
    temp='dir_${curdir}_${curlevel}'      # Note single quotes!
    x=$(eval echo $temp)
    eval $x=$PWD
    cd 
}

curdir=1
curlevel=0
change_dir /tmp

curdir=2
curlevel=4
change_dir $HOME

echo $dir_1_0
echo $dir_2_4
pwd

Output:

输出:

/Users/jleffler/tmp/soq
/tmp
/Users/jleffler

The recorded names are the names of the directory being left, not the one you arrive at.

记录的名称是留下的目录的名称,而不是您到达的名称。

回答by Paused until further notice.

The secure way to do this is to use indirection, associative arrays (Bash 4), functions or declare:

这样做的安全方法是使用间接、关联数组(Bash 4)、函数或declare

Use declare:

使用declare

declare $temp=$name_of_directory

Use indirection:

使用间接:

bar=42
foo=bar
echo ${!foo}
IFS= read -r $foo <<< 101
echo ${!foo}

Please take note of the security implications of eval.

请大家注意的的安全隐患eval