Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

php

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

The script get_fifobuild.php returns an integer which I need to assign into a bash variable. I ll appreciate if someone help me out.

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;

output:

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

no luck yet

like image 628
sakhunzai Avatar asked Jun 15 '12 12:06

sakhunzai


People also ask

How do I run a PHP file in bash?

Put that at the top of your script, make it executable ( chmod +x myscript. php ), and make a Cron job to execute that script (same way you'd execute a bash script). You can also use php myscript.

Can you assign variables in bash?

A variable in bash can contain a number, a character, a string of characters. You have no need to declare a variable, just assigning a value to its reference will create it.

How do I declare a string variable in bash?

To initialize a string, you directly start with the name of the variable followed by the assignment operator(=) and the actual value of the string enclosed in single or double quotes. Output: This simple example initializes a string and prints the value using the “$” operator.


1 Answers

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.

like image 92
John Lawrence Avatar answered Oct 06 '22 14:10

John Lawrence