我是一名帶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