我是使用Shell脚本的新手,今天学到了很多东西.
这是对该问题的扩展Assigning values printed by PHP CLI to shell variables
我得到了在shell脚本中读取变量的解决方案.现在如何操作数组?如果我在PHP代码中准备一个数组并进行打印,然后在我的shell中回显,它将显示Array.如何在shell脚本中访问该数组?我尝试了how to manipulate array in shell script中给出的解决方案
使用以下代码:-
PHP代码
$neededConstants = array("BASE_PATH","db_host","db_name","db_user","db_pass");
$associativeArray = array();
foreach($neededConstants as $each)
{
$associativeArray[$each] = constant($each);
}
print $associativeArray;
外壳代码
function getConfigVals()
{
php $PWD'/developer.php'
}
cd ..
PROJECT_ROOT=$PWD
cd developer
# func1 parameters: a b
result=$(getConfigVals)
for((cnt=0;cnt
do
echo ${result[$cnt]}" - "$cnt
done
我得到这个输出:
Array - 0
- 1
- 2
- 3
- 4
而我想得到这个:
Array
BASE_PATH - /path/to/project
db_host - localhost
db_name - database
db_user - root
db_pass - root