luca_lu
luca_lu

Reputation: 21

Pass result from bash back to PHP using exec

I to want call a bash function with 'exec'/'bash_exec' in PHP, and then get the result back into PHP code. Something like:

<?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");
..
..
?>

How should I do it?

Upvotes: 1

Views: 1385

Answers (2)

PizzaMartijn
PizzaMartijn

Reputation: 122

you could also run the bash command like this:

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

backticks makes php execute the string inside and return the result

Upvotes: 0

Aurelio De Rosa
Aurelio De Rosa

Reputation: 22152

Take a look at the signature of the function:

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

If you use the second parameter of the exec function, you'll get the output. More details here: http://php.net/manual/en/function.exec.php

Upvotes: 1

Related Questions