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

通过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");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向