Execute php script from bash , assign output to a bash variable

后端 未结 2 1618
一整个雨季
一整个雨季 2021-01-11 13:13

I have a bash script which need to execute some php scripts and to get back the results e.g

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


        
相关标签:
2条回答
  • 2021-01-11 13:59
    foobar=`/usr/bin/php -f $HOME/lib/get_fifobuild.php`
    

    note: these are backticks.

    0 讨论(0)
  • 2021-01-11 14:13
    myvariable=$(/usr/bin/php -f $HOME/lib/get_fifobuild.php)
    

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

    Update:

    This will assign the output of the command to the variable, but as you are still having problems I can perhaps suggest a few things:

    1. you have 'get_builds.php' and 'get_fifobuild.php' elsewhere.

    2. check that $HOME is being set correctly. You may be better with a different variable name here as that environment variable generally is set to your home directory. This however is unlikely to be the problem as you are getting output from the script.

    3. Is the text you gave the exact contents of your PHP file? If you have quotes around phpinfo() for example it will cause the output to just be the string "phpinfo()". In fact, you do not need the echo at all and could make the contents of your PHP file as follows.

    get_fifobuild.php:

    <?php
        phpinfo();
    ?>
    

    Update 2:

    Try changing your 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 the variables in the 'log' and 'echo' lines. The problem you were having was that only the first line of your php output was being logged.

    0 讨论(0)
提交回复
热议问题