drot98385 2012-10-05 10:12
浏览 119
已采纳

通过php执行shell命令并在浏览器中显示它?

I would like to execute a shell command through php and display it in a browser. Is there anyway to do so? here is my php code : [test.php]

<?php
$number=$_GET["num"];
$date=$_GET["date"];
$output = shell_exec('egrep -w  '2012-09-01|974' /home/myquery_test/log/push.log');
echo "<pre>$output</pre>";
?>

When I run this(test.php) file from browser nothing shows up. But when i change the

$output = shell_exec('ls')

its working fine!! Why isn't the egrep/grep command not working??

  • 写回答

1条回答 默认 最新

  • drlnwji79147769 2012-10-05 10:24
    关注

    The egrep command isn't working, because you're using single quotes as a string constant delimiter: 'egreep -w' <==> 2012-09-01|974' <==> /home/myquery_test/log/push.log' <==
    Just use double quotes in the string, or as string delimiters OR escape the quotes.

    shell_exec('egrep -w  \'2012-09-01|974\' /home/myquery_test/log/push.log');
    shell_exec('egrep -w  "2012-09-01|974" /home/myquery_test/log/push.log');
    shell_exec("egrep -w  '2012-09-01|974' /home/myquery_test/log/push.log");
    

    And, to avoid not getting the warnings and errors that would have brought this issue to light when testing, set your ini to E_STRICT|E_ALL, and fix the warnings, rather then ignoring them. [teasingly: after you're done with that, you might want to consider accepting some answers]I see you've accepted a lot while I was typing this post up :)

    Using variables in your command:

    $output = shell_exec("tail -f | egrep '$number.*$date' /var/www/myquery_test/log/push.log");
    $output = shell_exec('tail -f | egrep "'.$number.'.*'.$date.'" /var/www/myquery_test/log/push.log');
    $output = shell_exec("tail -f | egrep \"$number.*$date\" /var/www/myquery_test/log/push.log");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀