Run a bash called from php and return a string

Viewed 73

Hello I have the following PHP function:

public function index()
{        
  $return_var = exec('/home/iosef/createinstanceinfolder.sh');
  print_r($return_var);
}

Which is calling the following bash script in ubuntu 20.04:

#!/bin/bash

$my_array=(foo bar)
$my_array[0]=foo

How can I return this array to PHP?

1 Answers

Exec takes 2 more parameters.

try:

  $return_var = exec('/home/iosef/createinstanceinfolder.sh', $output, $retval);

Then print_r($output);

Also $output basically returns everything displayed by your script so in your script echo as a json or print_r your array in the end

Related