1

I have a PHP script that is run via CLI. In turn I want that script to call a bash script, but preferably I would like to break up the BASH requests so I can see the action as it is happening.

I can seem to set Environmental variables and they seem to exist between shell_exec() functions. But even when I have a source file like:

source ./bashes/functions.sh

And in the source file I use "export -f function-name" to export the functions in the script before executing the next line, the next line does not see the functions.

  $file = file('./bashes/bash_testing.sh',FILE_SKIP_EMPTY_LINES);

  //we want to watch this in realtime so write each line seperately to shell.
  foreach($file as $command) {
    $output->writeln(shell_exec($command));
  }

The function $output->writeln is a helper function just to echo the returned result. But basically the error I get is

sh: now: command not found

now is defined as a function in the included bash_testing.sh shell script.

Anyone know how I can resolve this issue?

Here is the source to the ./bashes/functions.sh file:

function now {
  date -u +%T
}

export -fx now
DevelumPHP
  • 155
  • 1
  • 8
  • 1
    That won't work because a process cannot modify its parent's environment. You can export a variable only to your children. What is it that you really want to do? I don't understand how this would help you "see the action" – rici Jan 25 '16 at 20:53
  • That is a real bummer. Seems odd that the environment has to change for every shell_exec() too bad there isn't a way to keep them all in the same scope while inside another shell script. Do you believe this is an issue with all non-bash environments? Like Python would exhibit the same issue right. – DevelumPHP Jan 25 '16 at 21:46
  • Absolutely. Even php :) – rici Jan 25 '16 at 21:56
  • 1
    You could create a bash process and write to its stdin. But that's unlikely to actually solve your real problem, although I don't know what you are actually trying to accomplish. – rici Jan 25 '16 at 21:58
  • This is actually all I need, but I would love to see an example where you could also send multiple commands into the stdin while the shell is doing something, that way everything stays in scope. Doing the popen() example which is the first accepted answer here works for me now: https://stackoverflow.com/questions/20107147/php-reading-shell-exec-live-output – DevelumPHP Jan 25 '16 at 22:30

1 Answers1

1

There is a way to maintain a single bash shell, execute commands and handle the return. I recently published a project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS

I would suggest not triggering a bash script but rather trigger the induvidual commands. That way you can handle the return and not have to build exception handling in bash.

After downloading you would simply use the following code:

//get a real bash shell.
$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', false);

$return1  = $shell->exeCmd($command1);
//logic to handle the return

$return2  = $shell->exeCmd($command2);
//logic to handle the return

..... etc

MerlinTheMagic
  • 575
  • 5
  • 16