简体   繁体   English

从/ dev / tty读取时,PHP(cli)吞下一个字节

[英]PHP (cli) swallows a byte when reading from /dev/tty

I am having an issue with PHP (cli) when using /dev/tty as the file. 当使用/dev/tty作为文件时,我遇到了PHP(cli)的问题。 The actual scenario is slightly more complex and is not mentioned here to keep the question simple. 实际情况稍微复杂一些,为了使问题简单,这里没有提到。

To run PHP code from STDIN, I am able to run the command as follows, 要从STDIN运行PHP代码,我能够运行如下命令,

$ php
<?php
echo "hello world\n";
?>
<CTRL-D>
hello world
$

However, when run through /dev/tty , the following happens, 但是,当运行/dev/tty ,会发生以下情况,

$ php /dev/tty
<?php
echo "hello world\n";
?>
<CTRL-D>
?php
echo "hello world\n";
?>
$

NOTE: The first character < is simply lost and php processes the input as plain text. 注意:第一个字符<简单地丢失,php将输入作为纯文本处理。 We can simplify the test to the following, 我们可以将测试简化为以下内容,

Working 工作

$ php
abcd
abcd
$

Not Working 不工作

$ php /dev/tty
abcd
bcd
$

I have run strace to check if php reads the input correctly and it seems to be so, as shown below, 我已经运行strace来检查php是否正确读取了输入,它似乎是如此,如下所示,

$ strace -o /tmp/1 php /dev/tty ; grep "bcd" /tmp/1
abcd
<CTRL-D>
bcd
read(4, "abcd\n", 4096)                 = 5
write(1, "bcd\n", 4)                    = 4
$

This seems pretty simple and, I feel, I am missing something very basic here. 这看起来很简单,我觉得,我在这里遗漏了一些非常基本的东西。

Does anyone know why PHP (cli) behaves this way when reading from /dev/tty ? 有没有人知道为什么PHP(cli)从/ dev / tty读取时会这样做?

Additional Info:- 附加信息:-

  • PHP Versions: 5.3.3 (cli) and 5.5.20 (cli) PHP版本:5.3.3(cli)和5.5.20(cli)
  • OS: Linux (CentOS 6.4 and Fedora 19) 操作系统:Linux(CentOS 6.4和Fedora 19)

php /dev/tty \\<?php echo "hi\\n"; ?> hi

Escaping the < worked. 逃避<工作。 I don't know why though :D Probably related to the fact that this is in ran in bash. 我不知道为什么:D可能与这个在bash中运行的事实有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM