Can I get variables from a shell script into php variables

I am helpng somebody run a shell script from php and he needs to get the variables created in the script back into the php code when the shell script is finished.

I have suggested he creates the variables in php and send those to the shell script instead; he could do this but would prefer to use the shell script if possible.

This is the code I am using to call the shell script:


$font = "Helvetica-Bold";
$pointsize = "50";
$textcolor = "red";
$undercolor = "blue";
$bordercolor = "yellow";
$angle = "30";
$resize = "100";

// Run the script
exec("/home/user/public_html/fred -f $font -p $pointsize -t $textcolor -u $undercolor -b $bordercolor -a $angle -r $resize fred.png 2>&1", $array);
//Display any errors
echo "<br><pre>".print_r($array)."<br>";
echo "</pre>";

Well, exec() returns the last line of the output from the shell command, so if the shell script is returning output like “value” or “a, b, c” you can capture this output in a var or array as needed.

sorry, I just saw you’re outputting a png - look into passthru() if you want to output an image to the browser (binary stream).

Thats something to think about old_iron and I will do some tests.

I should have put some more info in my post - the image is a CAPTCA image and the characters are generated in the shell script. He needs the characters generated to check against in the form.
The shell script is being written so it can be used by different software.

I have suggested he has the image in the memory only and not save it. The code he has written at the moment has everything hard coded as a test.

I still think the way to go is get php ( or whatever other software ) to generate the random characters and then send them to the shell script.

how about this

test.sh


#!/bin/sh

re=x
echo $re
export re

and the php


<?php

function render()
{
	$cmd = "sh test.sh";

    system($cmd);

    echo getenv("re");
}

render();

HOw about shell_exec() http://fi.php.net/shell_exec ?

Thanks for the replys; I have not tried shell_exec() yet and we are trying to think of another way to solve the problem - lateral thinking !

Ernie1 - I only get one x displayed and that is coming from the shell script; if I comment out echo $re I get nothing displayed.
I would assume I should have xx One x from the shell script and the other from the php code ?

I do not get an output from shell_exec() either; I wonder if its a server setup problem.

If you are using some hosting company to host your pages then I think shell_exec and maybe some others also can be forbidden. If you are running own server then it might be some settings also. I dont know more about these settings… I just know some hosting companies disables these.

you can write to a file then read from that file


#!/bin/sh

re=x
echo $re > test

That works Ernie1; I end up with a file called test containing x

I will pass on the info and see how it goes :smiley: