控制台应用程序(例如Java)如何识别“标准输入”的来源?溪流?
如果我在 Linux 命令行上运行“python”,并且不提供任何命令行参数,则程序会显示欢迎消息并等待用户输入。我假设在幕后,程序将消息发送到“标准输出”流,并对“标准输入”流执行阻塞读取。
但是,如果我通过管道传输另一个进程的输出来间接调用 python(例如 echo "hello" | python),则 python 不会输出该消息。不知何故,它可以区分原始场景(a)“标准输入”流由其他进程填充和(b)“标准输入”流从键盘填充的情况。
这是如何实现的?我一直认为这些信息不可用于应用程序。 Java 应用程序如何才能实现这种差异化?
If I run 'python' on the linux command line, and I don't provide any command line arguments, the program displays a welcome message and waits for user input. I would assume that under the hood, the program sends the message to the 'standard output' stream and performs a blocking read on the 'standard input' stream.
If, however, I invoke python indirectly by piping the output from another process (e.g. echo "hello" | python), python does NOT output the message. Somehow, it can tell the difference between the original scenario (a) where the 'standard input' stream is being populated by some other process and (b) where the 'standard input' stream is populated from the keyboard.
How is this accomplished? I had always thought that this information is not available to an application. How would a Java application be able to achieve this differentiation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Python:
我不知道使用默认库的 Java 等效项; Jython 似乎使用本机代码 (jna-posix)。
更新 - 这里有两种可能性:
null
否则(请参阅此处和此处)Python:
I'm not aware of a Java equivalent using the default libraries; Jython appears to use native code (jna-posix).
Update - here's two possibilities:
null
otherwise (see here and here)