在操作shell脚本数组(通过PHP-CLI印) [英] Manipulating an array (printed by php-cli) in shell script
问题描述
我与shell脚本一个新手,我今天学到了很多东西。
这是一个扩展这个问题<一href=\"http://stackoverflow.com/questions/4376695/assigning-values-printed-by-php-cli-to-shell-variables\">Assigning通过PHP CLI印制值shell变量
我得到了解决阅读我的shell脚本的变量。现在,如何操纵一个数组?如果我prepare数组在我的PHP code和打印,并在我的壳回声,它显示阵列。如何访问在shell脚本数组?我试着在<特定解决方案href=\"http://stackoverflow.com/questions/2352530/how-to-manipulate-array-in-shell-script/2353298#2353298\">how操纵在shell脚本数组
用下面的code: - 结果
的 PHP code 的
$ neededConstants =阵列(BASE_PATH,DB_HOST,DB_NAME,DB_USER,DB_PASS);$ associativeArray =阵列();
的foreach($ neededConstants每个$)
{
$ associativeArray [$每个] =常数($个);
}打印$ associativeArray;
的壳牌code 的
函数getConfigVals()
{
PHP的$ PWD'/ developer.php
}
CD ..
PROJECT_ROOT = $ PWD
CD开发商#func1的参数:A B
结果= $(getConfigVals)为((CNT = 0; CNT&下; $ {#结果}; CNT ++))
做
回声$ {结果[$ CNT]} - $ CNT
DONE
我得到这样的输出: -
阵列 - 0
- 1
- 2
- 3
- 4
而我想要得到这样的: -
阵列
BASE_PATH - /路径/要/项目
DB_HOST - 本地主机
DB_NAME - 数据库
DB_USER - 根
DB_PASS - 根
您应该调试你的 PHP脚本第一家生产有效阵列的内容,code
打印$ associativeArray;
只会让你的输出如下:
$ PHP test.php的
排列
您可以简单地打印关联数组在foreach循环:
的foreach($ associativeArray为$关键=&GT; $ VAL){
回声$键:$ VAL \\ N的;
}
让变量名称由+内容分隔列表:
$ PHP test.php的
BASE_PATH:1
DB_HOST:2
DB_NAME:3
DB_USER:4
DB_PASS:5
至于在 shell脚本,我建议使用的简单易懂的外壳构造的,然后获取到高级的(如 $ {#结果}
)正确地使用它们。
我曾尝试以下bash脚本摆脱PHP脚本输出变量shell脚本:
#设置读取COMAND字段分隔符
IFS =:#解析PHP脚本,通过读命令的输出
PHP的$ PWD'/ test.php的'|而阅读-r关键VAL;做
回声$键= $ VAL
DONE
I am a newbie with shell scripts and I learnt a lot today. This is an extension to this question Assigning values printed by PHP CLI to shell variables
I got the solution to read a variable in my shell script. Now how to manipulate an array? If I prepare an array in my PHP code and print it, and echo in my shell, it displays Array. How to access that array in the shell script? I tried the solution given in how to manipulate array in shell script
With the following code:-
PHP code
$neededConstants = array("BASE_PATH","db_host","db_name","db_user","db_pass");
$associativeArray = array();
foreach($neededConstants as $each)
{
$associativeArray[$each] = constant($each);
}
print $associativeArray;
Shell code
function getConfigVals()
{
php $PWD'/developer.php'
}
cd ..
PROJECT_ROOT=$PWD
cd developer
# func1 parameters: a b
result=$(getConfigVals)
for((cnt=0;cnt<${#result};cnt++))
do
echo ${result[$cnt]}" - "$cnt
done
I get this output:-
Array - 0
- 1
- 2
- 3
- 4
Whereas I want to get this:-
Array
BASE_PATH - /path/to/project
db_host - localhost
db_name - database
db_user - root
db_pass - root
You should debug your PHP script first to produce the valid array content, code
print $associativeArray;
will just get you the following output:
$ php test.php
Array
You can simply print the associative array in a foreach loop:
foreach ( $associativeArray as $key=>$val ){
echo "$key:$val\n";
}
giving a list of variable names + content separated by ':'
$ php test.php
BASE_PATH:1
db_host:2
db_name:3
db_user:4
db_pass:5
As for the shell script, I suggest using simple and understandable shell constructs and then get to the advanced ones (like ${#result}
) to use them correctly.
I have tried the following bash script to get the variables from PHP script output to shell script:
# set the field separator for read comand
IFS=":"
# parse php script output by read command
php $PWD'/test.php' | while read -r key val; do
echo "$key = $val"
done
这篇关于在操作shell脚本数组(通过PHP-CLI印)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!