Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 !

share|improve this question
 
what is that 'e' doing after first echo statement ? –  Icarus3 Dec 30 '12 at 16:52
 
sorry, there is no "e" fixed the Q.. + when i ./testscript.sh it works ok and print the 2 lines –  ilansch Dec 30 '12 at 16:55
add comment

3 Answers

up vote 2 down vote accepted

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("\n", $output);
share|improve this answer
 
Or better to use another function, that return output always. –  gaRex Dec 30 '12 at 17:48
1  
@gaRex, of course OP would want to check the return value of the script, and make sure $output is not empty before doing implode(), but that all depends on how OP codes the shell script. –  cryptic ツ Dec 30 '12 at 17:51
 
then it's even may be better to use proc_* functions stack. –  gaRex Dec 30 '12 at 17:54
add comment

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 \n, 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;
}
?>
share|improve this answer
add comment

In your php script try

echo system('/ilantest/testscript.sh');
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.