Using -B option blocks interpreter.

huangapple go评论67阅读模式
英文:

Using -B option blocks interpreter

问题

当尝试从Linux shell执行PHP解释器时,使用-B选项,提供的begin_code被执行,但进程被阻塞,主脚本(f2.php)不被执行。

问题重现:

$ php -v
PHP 8.1.13 (cli) (built: Nov 22 2022 14:42:07) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.1.13, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.13, Copyright (c), by Zend Technologies
$ cat f2.php 
<?php
 echo "f2 running.\n";
?>
$ php f2.php 
f2 running.
$ php  -B 'echo "init\n";' f2.php 
init

然后终端被阻塞。我随后必须使用Ctrl+C返回到提示符。

我期望脚本f2.php也被执行。我做错了什么?

英文:

When trying to execute PHP interpreter from Linux shell, with the -B option, the supplied begin_code is executed but the process blocks and the main script (f2.php) is not executed.

Problem reproduction:

$ php -v
PHP 8.1.13 (cli) (built: Nov 22 2022 14:42:07) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.1.13, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.13, Copyright (c), by Zend Technologies
$ cat f2.php 
<?php
 echo "f2 running.\n";
?>
$ php f2.php 
f2 running.
$ php  -B 'echo "init\n";' f2.php 
init

And the shell blocks. I must then use ctrl+C to return to prompt.
I expected that the script f2.php is also executed.
What am I doing wrong?

答案1

得分: 1

当使用-B参数时,PHP进入交互模式。要执行f2.php,您需要添加-F参数,然后只有在按下ENTER键后才会运行您的脚本:

me:~$ php -B 'echo "init\n";' -F f2.php
init
<ENTER>
f2 running.
<ENTER>
f2 running.
^C
me:~$

要让PHP进入非交互模式,可以将内容通过管道传递给php命令。PHP将运行初始化代码和您的脚本,然后终止:

me:~$ echo | php -B 'echo "init\n";' -F f2.php
init
f2 running.
me:~$
英文:

When using the -B argument, PHP enters interactive mode. To execute f2.php, you need to add the -F parameter but then it will only run your script after you hit the ENTER key:

me:~$ php -B &#39;echo &quot;init\n&quot;;&#39; -F f2.php
init
&lt;ENTER&gt;
f2 running.
&lt;ENTER&gt;
f2 running.
^C
me:~$

To "trick" PHP into non-interactive mode, pipe something into the php command. PHP will then run the init code and your script, and then terminate:

me:~$ echo | php -B &#39;echo &quot;init\n&quot;;&#39; -F f2.php
init
f2 running.
me:~$

huangapple
  • 本文由 发表于 2023年3月12日 08:50:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定