I have php script on my redhat that im logging in as root via telnet client.
My PHP script to run the bash script is(functions.inc):
<?php
exec('/ilantest/testscript.sh');
?>
My Bash script:
#!/bin/bash
echo "Hello world"
echo "Whats going on ?"
And when i do: php functions.inc - I get the following:
Whats going on ?[root@X ilantest]#
Why i dont see the first line ?
Thanks !
exec() see http://us3.php.net/manual/en/function.exec.php#refsect1-function.exec-returnvalues
The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function. To get the output of the executed command, be sure to set and use the output parameter.
So:
exec('/ilantest/testscript.sh', $output);
echo implode("
", $output);
In your php script try
echo system('/ilantest/testscript.sh');
Only the last line will be printed if you don't specify any arguments to receive the output. From exec() manual:
If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as , is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().
You can pass an array to receive all lines:
<?php
$lines = array();
exec('/ilantest/testscript.sh', $lines);
foreach($lines as $i) {
echo $i;
}
?>