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

1 Answer 1

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 ;)

Sign up to request clarification or add additional context in comments.

1 Comment

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.