通过PHP执行shell命令并在浏览器中显示它? [英] Execute a shell command through php and display it in browser?
问题描述
我想通过php执行一个shell命令并在浏览器中显示它。无论如何要这样做?
这里是我的php代码:[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>;
?>
当我从浏览器运行这个(test.php)文件时,什么都没有显示出来。但是,当我改变
$ output = shell_exec('ls')
其工作正常!为什么egrep / grep命令不起作用? 使用单引号作为字符串常量分隔符:'egreep -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 -w2012-09-01 | 974/home/myquery_test/log/push.log');
shell_exec(egrep -w'2012-09-01 | 974'/home/myquery_test/log/push.log);
而且,为了避免在测试时不会得到警告和错误,将你的ini设置为 E_STRICT | E_ALL
,然后修正警告,而不是忽略它们。 [逗趣地:在你做完这些之后,你可能要考虑接受一些答案] 我看到你在接受这篇文章时接受了很多:
在命令中使用变量:
$ b
$ 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);
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??
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");
这篇关于通过PHP执行shell命令并在浏览器中显示它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!