在 bash shell 脚本中初始化动态变量(变量变量)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4385097/
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:03:36  来源:igfitidea点击:

Initiating dynamic variables (variable variables) in bash shell script

linuxbashshellvariables

提问by Sandeepan Nath

I am using PHP CLI through bash shell. Please check Manipulating an array (printed by php-cli) in shell scriptfor details.

我正在通过 bash shell 使用 PHP CLI。有关详细信息,请检查在 shell 脚本中操作数组(由 php-cli 打印)

In the following shell code I am able to echo the key- valuepairs that I get from the PHP script.

在下面的 shell 代码中,我可以回显从 PHP 脚本中获得的key-value对。

IFS=":"

# parse php script output by read command
php $PWD'/test.php' | while read -r key val; do
    echo $key":"$val
done

Following is the output for this -

以下是此输出 -

BASE_PATH:/path/to/project/root
db_host:localhost
db_name:database
db_user:root
db_pass:root

Now I just want to initiate dynamic variables inside the while loop so that I can use them like $BASE_PATHhaving value '/path/to/project/root', $db_hosthaving 'localhost'

现在我只想在 while 循环中启动动态变量,这样我就可以像$BASE_PATH拥有 value一样使用它们 '/path/to/project/root'$db_host拥有'localhost'

I come from a PHP background. I would like something like $$key = $valof PHP

我来自 PHP 背景。我想要类似$$key = $valPHP 的东西

回答by Paused until further notice.

Using evalintroduces security risks that must be considered. It's safer to use declare:

使用会eval引入必须考虑的安全风险。使用更安全declare

# parse php script output by read command
while IFS=: read -r key val; do
    echo $key":"$val
    declare $key=$val
done < <(php $PWD'/test.php')

If you are using Bash 4, you can use associative arrays:

如果您使用的是 Bash 4,则可以使用关联数组:

declare -A some_array
# parse php script output by read command
while IFS=: read -r key val; do
    echo $key":"$val
    some_array[$key]=$val
done < <(php $PWD'/test.php')

Using process substition <()and redirecting it into the doneof the whileloop prevents the creation of a subshell. Setting IFS for only the readcommand eliminates the need to save and restore its value.

使用过程substition<()并重定向到done了中while环防止子shell的创建。仅为read命令设置 IFS消除了保存和恢复其值的需要。

回答by Martin Kosek

You may try using the evalconstruct in BASH:

您可以尝试evalBASH 中使用构造:

key="BASE_PATH"
value="/path/to/project/root"
# Assign $value to variable named "BASE_PATH"
eval ${key}="${value}"

# Now you have the variable named BASE_PATH you want
# This will get you output "/path/to/project/root"
echo $BASE_PATH

Then, just use it in your loop.

然后,只需在您的循环中使用它。



EDIT: this read loop creates a sub-shellwhich will not allow you to use them outside of the loop. You may restructure the read loop so that the sub-shell is not created:

编辑:此读取循环创建了一个子外壳,不允许您在循环外使用它们。您可以重构读取循环,以便不创建子外壳:

# get the PHP output to a variable
php_output=`php test.php`

# parse the variable in a loop without creating a sub-shell
IFS=":"
while read -r key val; do
    eval ${key}="${val}"
done <<< "$php_output"

echo $BASE_PATH