【发布时间】:2016-03-30 20:24:51
【问题描述】:
我在 Debian 8.2 上运行 lighttpd 1.4.35。我有一个非常简单的带有 php 代码 (php5) 的 html 文件,它调用 bash 脚本并打印输出:
<html>
<body>
<?php
$res = shell_exec("/var/www/html/run.sh");
echo $res . " is the result";
?>
</body>
</html>
如果在 firefox 上调用该 html 文件,则输出为
is the result
如果我直接使用该文件 (php index.php) 运行 php,则输出为
<html>
<body>
13.00
is the result</body>
</html>
那么,结果在哪里丢失了?
编辑: 来自firefox的网页源代码是
<html>
<body>
is the result</body>
</html>
编辑:已解决。 bash 脚本使用“~”,当脚本从网络服务器运行时,它会扩展到错误的目录。
【问题讨论】:
-
尝试使用
$res = shell_exec("sh /var/www/html/run.sh");运行 -
exec 函数“仅”返回标准输出的内容。将 stderr 重定向到 stdout 时是否收到错误消息?
$res = shell_exec("/var/www/html/run.sh 2>&1"); -
@VolkerK - 就是这样,谢谢。 shell脚本使用'~',当直接在bash中运行php时,它被扩展为正确的目录,但当web服务器运行脚本时,它指向错误的目录。愚蠢的我;)
-
比预期的要简单 ;-) 那我会回答的。