2

I want the PHP equivalent of the solution given in assigning value to shell variable using a function return value from Python

In my php file, I read some constant values like this:-

$neededConstants = array("BASE_PATH","db_host","db_name","db_user","db_pass");
foreach($neededConstants as $each)
{
     print constant($each);
}

And in my shell script I have this code so far:-

function getConfigVals()
{

 php $PWD'/developer.php'

    //How to collect the constant values here??
 #echo "done  -  "$PWD'/admin_back/developer/developer.php'
}

cd ..
PROJECT_ROOT=$PWD
cd developer

# func1 parameters: a b
getConfigVals 

I am able to execute the file through shell correctly.

To read further on what I am trying to do please check Cleanest way to read config settings from PHP file and upload entire project code using shell script

Updates

Corrected configs=getConfigVals replaced with getConfigVals

Solution

As answered by Fritschy, it works with this modification:-

PHP code -

function getConfigVals()
{
    php $PWD'/developer.php'
    #return $collected
    #echo "done  -  "$PWD'/admin_back/developer/developer.php'
}

shell code -

result=$(getConfigVals)
echo $result
Community
  • 1
  • 1
Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144

1 Answers1

3

You have to execute the function and assign what is printed to that variable:

configs=$(getConfigVals)

See the manpage of that shell on expansion for more ;)

Marcus Borkenhagen
  • 6,536
  • 1
  • 30
  • 33
  • Thanks that works. But I still have to explode the result. Is there any single step to initiate many variables in my shell script? I want to initiate exactly those constants (variables in shell script) (as many constants in my php file -> those many variables with same values in my shell script) – Sandeepan Nath Dec 07 '10 at 12:54