Run PHP script from bash, assign output to bash variable

I have a bash script that needs to execute some php scripts and return the results, e.g.

#!/bin/bash /usr/bin/php -f $HOME/lib/get_fifobuild.php 

The script get_fifobuild.php returns the integer that I need to assign to the bash variable. I would appreciate it if anyone would help me.

thanks:)

Edit: php show.php

 <?php echo phpinfo(); exit; ?> 

bash script:

 #!/bin/bash HOME=`dirname $0`; log(){ NEW_LOG=$HOME/logs/cloud-`date +%d_%m_%Y`.log echo $1 >> $NEW_LOG } log "Date: `date`"; data=$(/usr/bin/php -f $HOME/lib/show.php); log $data; 

exit:

 Date: Fri Jun 15 19:16:00 PKT 2012 phpinfo() 

until succeed

+6
source share
2 answers
 myvariable=$(/usr/bin/php -f $HOME/lib/get_fifobuild.php) 

Assign the output from your php script to a variable called "myvariable".

Update

This will assign the output of the command to a variable, but since you still have problems, I can suggest a few things:

  • you have get_builds.php and get_fifobuild.php elsewhere.

  • check if $ HOME is installed correctly. You may be better off with a different variable name here, since this environment variable is usually set to your home directory. This, however, is unlikely to be a problem, as you get the output from the script.

  • Is the text that you provided the exact contents of your PHP file? If you have quotes around phpinfo() , for example, this will result in the output being just the string "phpinfo ()". In fact, you don't need echo at all, and it can do the contents of your PHP file as follows.

get_fifobuild.php:

 <?php phpinfo(); ?> 

Update 2:

Try changing the script to:

 #!/bin/bash HOME=`dirname $0`; log(){ NEW_LOG=$HOME/logs/cloud-`date +%d_%m_%Y`.log echo "$1" >> $NEW_LOG } log "Date: `date`"; data=$(/usr/bin/php -f $HOME/lib/show.php); log "$data"; 

Basically adding double quotes around variables in the lines 'log' and 'echo'. The problem you ran into was that only the first line of your php output was written.

+8
source
 foobar=`/usr/bin/php -f $HOME/lib/get_fifobuild.php` 

Note: these are reverse steps.

0
source

Source: https://habr.com/ru/post/918176/


All Articles