bash PHP脚本exec bash脚本不打印所有bash行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14092500/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP script exec bash script does not print all bash lines
提问by ilansch
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):
我的 redhat 上有 php 脚本,我通过 telnet 客户端以 root 身份登录。
我运行 bash 脚本的 PHP 脚本是(functions.inc):
<?php
exec('/ilantest/testscript.sh');
?>
My Bash script:
我的 Bash 脚本:
#!/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]#
当我这样做时:
phpfunctions.inc - 我得到以下信息:发生了什么?[root@X ilantest]#
Why i dont see the first line ?
Thanks !
为什么我没有看到第一行?
谢谢 !
回答by cryptic ツ
exec()see http://us3.php.net/manual/en/function.exec.php#refsect1-function.exec-returnvalues
exec()见http://us3.php.net/manual/en/function.exec.php#refsect1-function.exec-returnvalues
The lastline 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 outputparameter.
在最后的命令的结果一致。如果您需要执行一个命令并且让命令中的所有数据不受任何干扰地直接传回,请使用 passthru() 函数。要获取执行命令的输出,请务必设置和使用输出参数。
So:
所以:
exec('/ilantest/testscript.sh', $output);
echo implode("\n", $output);
回答by Sergejs Ri?ovs
In your php script try
在你的 php 脚本中尝试
echo system('/ilantest/testscript.sh');
回答by P.P
Only the last line will be printed if you don't specify any arguments to receive the output. From exec()manual:
如果您没有指定任何参数来接收输出,则只会打印最后一行。从exec()手册:
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().
如果存在输出参数,则指定的数组将填充命令的每一行输出。此数组中不包含尾随空格,例如 \n。请注意,如果数组已经包含一些元素, exec() 将附加到数组的末尾。如果您不希望函数追加元素,请在将数组传递给 exec() 之前在数组上调用 unset()。
You can pass an array to receive all lines:
您可以传递一个数组来接收所有行:
<?php
$lines = array();
exec('/ilantest/testscript.sh', $lines);
foreach($lines as $i) {
    echo $i;
}
?>

