通过BASH关联数组到PHP脚本 [英] Pass BASH associative arrays to PHP script

查看:106
本文介绍了通过BASH关联数组到PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能通过BASH关联数组将以argv到PHP脚本?

Is it possible to pass BASH associative arrays as argv to PHP scripts?

我有一个bash脚本,收集一些变量,以一个bash关联数组是这样的。在那之后,我需要将其发送到PHP脚本:

I have a bash script, that collects some variables to a bash associative array like this. After that, I need to send it to PHP script:

typeset -A DATA
DATA[foo]=$(some_bash_function "param1" "param2")
DATA[bar]=$(some_other_bash_function)

php script.php --data ${DATA[@]}

从PHP脚本,我需要访问阵列的方式如下:

From PHP script, i need to access the array in following manner:

<?php

    $vars = getopt("",array(
        "data:"
    ));

    $data = $vars['data'];

    foreach ($data as $k=>$v) {
          echo "$k is $v";
    }

?>

我已经试过

围绕奇怪的语法 - 数据参数如下从的伟大的职位有关从诺伯特KERI庆典阵列的如何强制传递的参数为一个数组:

What I've tried

Weird syntax around the --data parameter follows advice from a great post about bash arrays from Norbert Kéri how to force passed parameter as an array:

您没有信令要传递一个数组功能的方式。你得到N个位置参数,无需了解每一个数据类型的信息。

You have no way of signaling to the function that you are passing an array. You get N positional parameters, with no information about the datatypes of each.

然而,这sollution仍然没有关联数组工作 - 只值传递给函数。诺伯特·克里提出了跟进的文章有关,但其评估基于解决方案并不适合我,因为我需要实际的数组作为参数传递。

However this sollution still does not work for associative arrays - only values are passed to the function. Norbert Kéri made a follow up article about that, however its eval based solution does not work for me, as I need to pass the actual array as a parameter.

是我想要实现不可能的东西或者是有一些方法?谢谢!

Is the thing I'm trying to achieve impossible or is there some way? Thank you!

我有以下结构的一些PHP配置文件:

I have a few PHP configuration files of following structure:

<?php
return array(
    'option1' => 'foo',
    'option2' => 'bar'
)

我的bash脚本收集(通过庆典功能)并将其存储到bash的关联数组从用户输入数据。这个数组应作为参数传递给PHP脚本以后通过。

My bash script collects data from user input (through bash read function) and stores them into bash associative array. This array should be later passed as an argument to PHP script.

php script.php --file "config/config.php" --data $BASH_ASSOC_ARRAY

因此​​,而不是复杂的 SED 职能等等。我可以做简单的:

So instead of complicated seds functions etc. I can do simple:

<?php

    $bash_input = getopt('',array('file:,data:'));
    $data = $bash_input['data'];

    $config = require($config_file);
    $config['option1'] = $data['option1'];
    $config['option2'] = $data['option2'];

    // or

    foreach ($data as $k=>$v) {
         $config[$k] = $v;
    }

    // print to config file
    file_put_contents($file, "<?php \n \n return ".var_export($config,true).";");
?>

这是用于配置Laravel配置文件

This is used for configuring Laravel config files

推荐答案

不同的方法来@ WILL的

Different Approach to @will's

您的bash脚本:

typeset -A DATA
foo=$(some_bash_function "param1" "param2")
bar=$(some_other_bash_function)

php script.php "{'data': '$foo', 'data2': '$bar'}"

PHP脚本

<?php

    $vars = json_decode($argv[1]);

    $data = $vars['data'];

    foreach ($data as $k=>$v) {
          echo "$k is $v";
    }

?>

修改(更好的方法) 感谢@will

typeset -A DATA
DATA[foo]=$(some_bash_function "param1" "param2")
DATA[bar]=$(some_other_bash_function)

php script.php echo -n "{"; for key in ${!DATA[@]}; do echo - "'$key'":"'${DATA[$key]}'", | sed 's/ /,/g' ; done; echo -n "}"

这篇关于通过BASH关联数组到PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆