如何在php中传递参数来执行shell命令?

时间:2013-12-16 10:21:14

标签: php bash shell

以下是下面列出的计划。我试图从PHP运行shell命令,所以我写了以下代码:

<?php    
$argument1 = $argv[1];    
$output = shell_exec('sudo whois ');    
echo "<pre>$output</pre>";    
?> 

但每次执行命令时都会执行但不显示输出。仅显示选项。

我在shell中的命令是 php filename.php google.com

1 个答案:

答案 0 :(得分:2)

您没有在命令中使用传递参数。您需要在whois命令中使用参数:

<?php    
$argument1 = escapeshellarg($argv[1]);  
$output = shell_exec('whois '. $argument1);    
echo "<pre>$output</pre>";    
?>

PS:whois也可以在没有sudo的情况下进行调整。