保持了shell_exec之间的bash shell功能()请求 [英] Keep bash shell functions between shell_exec() requests

查看:152
本文介绍了保持了shell_exec之间的bash shell功能()请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是通过CLI运行PHP脚本。反过来我想该脚本调用bash脚本,但preferably我想分手的BASH请求,以便我可以看到,因为它正在发生的动作。

我似乎可以设置环境变量,他们似​​乎了shell_exec()函数的存在。但是,即使我有这样一个源文件:

源./bashes/functions.sh

和我使用出口-f函数名在执行下一行之前导出脚本函数在源文件中,下一行没有看到的功能。

  $文件=文件(./打击并可以击晕/ bash_testing.sh',FILE_SKIP_EMPTY_LINES);  //我们要实时观看这部这么写的每一行单独壳。
  的foreach($文件$命令){
    $输出 - > writeln(了shell_exec($命令));
  }

功能$输出 - > writeln是一个辅助函数只是呼应返回的结果。但基本上我得到的错误是

  SH:现在:命令未找到

现在被定义为包括bash_testing.sh shell脚本的功能。

任何人都知道我怎样才能解决这个问题呢?

下面是源到./bashes/functions.sh文件:

 函数现在{
  日期-u +%T
}现在出口-FX


解决方案

有是维护一个单一的bash shell中,执行命令和处理退货的方式。我最近出版的一期工程,使PHP获取和一个真正的Bash shell交互。在这里获得: https://github.com/merlinthemagic/MTS

我建议不触发一个bash脚本,而是触发induvidual命令。你可以办理退换货,而不是通过这种方式必须建立例外在bash处理。

下载您只需使用以下code之后:

  //得到一个真正的bash shell。
$ SHELL = \\ MTS \\工厂:: getDevices() - GT;的getLocalHost() - GT; getShell('庆典',FALSE);$ RETURN1 = $用shell> exeCmd($命令1);
//逻辑来处理退货$ RETURN2 = $用shell> exeCmd($命令2);
//逻辑来处理退货

.....等等

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

解决方案

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

这篇关于保持了shell_exec之间的bash shell功能()请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆