我是一名带shell脚本的新手,今天我学到了很多东西。 这是此问题的扩展Assigning values printed by PHP CLI to shell variables在shell脚本中操作数组(由php-cli打印)
我得到了解决方案来读取我的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;
shell代码
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
我得到这样的输出: -
Array - 0
- 1
- 2
- 3
- 4
而我想这一点: -
Array
BASE_PATH - /path/to/project
db_host - localhost
db_name - database
db_user - root
db_pass - root
感谢和+1建议首先使用简单和可理解的shell构造,然后到达advan ced的人 – 2010-12-08 07:31:30