首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP shell_exec() -不打印动态输出,只打印静态回显文本

PHP shell_exec() -不打印动态输出,只打印静态回显文本
EN

Stack Overflow用户
提问于 2011-07-22 01:38:51
回答 2查看 4.7K关注 0票数 2
代码语言:javascript
运行
复制
****UPDATE****
# The issue was within the wanpipemon script and the way that it interacts 
# with ioctl basically unless you want to edit the source for wanpipemon 
# and recompile it, it will not work. I have tried -S with root among 
# others and unless I am within console it throws a not authorized error 
# in the ioctl function.

好的,我有一个简单的shell脚本,它运行一个命令并输出FXO线电压,通常你必须一次得到一行,这个脚本将它们组合成一个可读的输出。(与这篇文章无关,但我想有人可能会问这个脚本是做什么的)

这是shell脚本:

代码语言:javascript
运行
复制
#!/bin/bash

LINE1=$(wanpipemon -i w1g1 -c astats -m 1 | grep -m1 VOLT | cut -d ":" -f 2-2)
LINE2=$(wanpipemon -i w1g1 -c astats -m 2 | grep -m1 VOLT | cut -d ":" -f 2-2)
LINE3=$(wanpipemon -i w1g1 -c astats -m 3 | grep -m1 VOLT | cut -d ":" -f 2-2)
LINE4=$(wanpipemon -i w1g1 -c astats -m 4 | grep -m1 VOLT | cut -d ":" -f 2-2)
LINE5=$(wanpipemon -i w1g1 -c astats -m 5 | grep -m1 VOLT | cut -d ":" -f 2-2)
LINE6=$(wanpipemon -i w1g1 -c astats -m 6 | grep -m1 VOLT | cut -d ":" -f 2-2)

echo "Line 1 Voltage: $LINE1"
echo "Line 2 Voltage: $LINE2"
echo "Line 3 Voltage: $LINE3"
echo "Line 4 Voltage: $LINE4"
echo "Line 5 Voltage: $LINE5"
echo "Line 6 Voltage: $LINE6"

通过终端在机器上运行脚本,它工作得非常好,真的很完美。在我的PHP脚本中,我只是这样做( php脚本与sh文件在同一服务器上):

代码语言:javascript
运行
复制
<?php
    $output = shell_exec('/usr/sbin/linesta.sh');
    echo "<pre>$output</pre>";
?>

我在运行该脚本的浏览器中收到的输出是:

代码语言:javascript
运行
复制
Line 1 Voltage:
Line 2 Voltage:
Line 3 Voltage:
Line 4 Voltage:
Line 5 Voltage:
Line 6 Voltage:

现在我已经确保了权限是正确的,我们应该已经知道这一点,因为它运行了50%的方式,就像我说过的那样,我知道脚本在机器上工作。

这真的很奇怪,因为它输出的是静态文本,而不是动态文本。命令'wanpipemon -i w1g1 -c astats -m *‘是我的交换机上模拟卡驱动程序的一部分,不需要超级用户来运行它(任何用户都可以运行该命令),所以它把我搞糊涂了。

任何帮助和/或调试建议都将非常感谢,到目前为止,我只尝试在shell脚本上仔细检查权限,在控制台(即linesta.sh)的服务器上运行该脚本,但我不确定还可以测试什么。我尝试了其他PHP命令,如exec()和system(),结果完全相同。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-22 01:42:46

“未找到文件”类型错误将写入stderr,并且不会被脚本中的字符串构建捕获,也不会被PHP捕获,除非您执行了stderr重定向:

代码语言:javascript
运行
复制
$output = shell_exec('/usr/sbin/linesta.sh 2>&1');
                                           ^^^^--- redirect stderr to stdout.
票数 2
EN

Stack Overflow用户

发布于 2011-07-22 01:43:43

shell_exec有很多特殊之处,我使用以下函数来获取所有输出

代码语言:javascript
运行
复制
/**
 * Executes the shell command and gets stdout and stderr streams
 *
 * @see http://www.php.net/manual/en/function.proc-open.php
 * @param string $cmd - command to be executed
 * @param string $cwd - folder
 * @param array $env - options
 * @return array()
 */
function shexec($cmd, $cwd = './', $env = array())
{
    $return_value = array(
         "exit"   => 1,       // exit 0 on ok
         "stdout" => "",      // output of the command
         "stderr" => "",      // errors during execution
    );

    $descriptorspec = array(
    0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
    1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
    2 => array("pipe", "w")   // stderr is a pipe
    );

    $process = proc_open(escapeshellcmd($cmd), $descriptorspec, $pipes, $cwd, $env);
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // 2 => readable handle connected to child stderr

    if (false === is_resource($process))
    {
        //echo("Sys::shexec() Error on proc_open, shell command \"$cmd\"");
    }
    else
    {
        $return_value['stdout'] = stream_get_contents($pipes[1]);
        $return_value['stderr'] = stream_get_contents($pipes[2]);

        fclose($pipes[0]);
        fclose($pipes[1]);
        fclose($pipes[2]);

        // It is important that you close any pipes before calling
        // proc_close in order to avoid a deadlock
        $return_value['exit'] = proc_close($process);
    }

    if(trim($return_value['stderr']) !== "") {
        //echo("Sys::shexec() \n\"$cmd\"\nERROR:\n" . $return_value['stderr']);
    }

    if(trim($return_value['stdout']) !== "") {
        //echo("Sys::shexec() \n\"$cmd\"\nOUTPUT:\n" . $return_value['stdout']);
    }

    return $return_value;

} // END FUNCTION shexec()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6780285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档