donglu3087 2016-01-25 18:41
浏览 35

在shell_exec()请求之间保留bash shell函数

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
  • 写回答

1条回答 默认 最新

  • douxia2053 2016-05-23 12:06
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记