Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
965 views
in Technique[技术] by (71.8m points)

linux - Assigning values printed by PHP CLI to shell variables

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...