英文:
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 'echo "init\n";' -F f2.php
init
<ENTER>
f2 running.
<ENTER>
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 'echo "init\n";' -F f2.php
init
f2 running.
me:~$
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论