15

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

sakhunzai
  • 13,900
  • 23
  • 98
  • 159
  • Possible duplicate of [Assigning values printed by PHP CLI to shell variables](http://stackoverflow.com/questions/4376695/assigning-values-printed-by-php-cli-to-shell-variables). There are many questions on SO about assigning output of a program to shell variables. Did you do a search? – Dennis Williamson Jun 15 '12 at 13:24
  • 1
    Yes I have done so , but no luck :( – sakhunzai Jun 15 '12 at 13:35
  • Did you search for "[bash assign variable](http://stackoverflow.com/search?q=bash+assign+variable)"? – Dennis Williamson Jun 15 '12 at 14:55

2 Answers2

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.

John Lawrence
  • 2,923
  • 17
  • 23
0
foobar=`/usr/bin/php -f $HOME/lib/get_fifobuild.php`

note: these are backticks.

kojiro
  • 74,557
  • 19
  • 143
  • 201
Rufinus
  • 29,200
  • 6
  • 68
  • 84
  • 2
    Please use the `$()` convention over the `\`\`` convention. It's more readable and backticks cannot be nested (whereas the dollar convention can). – cmh Jun 15 '12 at 14:01
  • Uh, apparently I cannot reverse my downvote because you didn't edit it *enough* so that it shows as having been edited. Sometimes SO is dumb. So I made a tiny edit. :-/ – kojiro Jun 15 '12 at 14:27