使用exec将结果从bash传递回PHP

时间:2011-10-23 22:25:16

标签: php bash

我想在PHP中使用'exec'/'bash_exec'调用bash函数,然后将结果返回到PHP代码中。类似的东西:

<?php
..
..

//bash funct. definitions
$my_bash_function_path='/usr/bin/ls';
$my_bash_param1='x1';
$my_bash_param2='dsfx1';

//clling bash function
exec( $my_bash_function_path." ".$my_bash_param1." ".$my_bash_param2 );

//doing something with a result of bash function
echo ("some_output_of_bash_function");
..
..
?>

我该怎么做?

2 个答案:

答案 0 :(得分:1)

看一下该功能的签名:

string exec ( string $command [, array &$output [, int &$return_var ]] )

如果您使用exec函数的第二个参数,您将获得输出。更多详情:http://php.net/manual/en/function.exec.php

答案 1 :(得分:0)

你也可以像这样运行bash命令:

$command=$my_bash_function_path." ".$my_bash_param1." ".$my_bash_param2;
$results=`$command`;
echo $results;

反引号使php执行内部字符串并返回结果