如何在 Perl 中刷新反引号中的输出?
如果我有这个 perl 应用程序:
print `someshellscript.sh`;
它会打印一堆内容并且需要很长时间才能完成,那么如何在 shell 脚本执行过程中打印该输出?
看起来 Perl 只会在完成时打印 someshellscript.sh 结果,有没有办法使输出在执行过程中刷新?
If I have this perl app:
print `someshellscript.sh`;
that prints bunch of stuff and takes a long time to complete, how can I print that output in the middle of execution of the shell script?
Looks like Perl will only print the someshellscript.sh result when it completes, is there a way to make output flush in the middle of execution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的问题是,使用反引号转义会将脚本存储到字符串中,然后打印该字符串。 因此,无法用打印来“刷新”。
使用 system() 命令应该连续打印输出,但您将无法捕获输出:
The problem here is that escaping with backticks stores your script to a string, which you then print. For this reason, there would be no way to "flush" with print.
Using the system() command should print output continuously, but you won't be able to capture the output:
您可能想要做的是这样的:
这将运行
someshellscript.sh
并打开一个读取其输出的管道。 while 循环读取脚本生成的每一行输出并将其打印出来。 有关详细信息,请参阅 open 文档页面。What you probably want to do is something like this:
This runs
someshellscript.sh
and opens a pipe that reads its output. Thewhile
loop reads each line of output generated by the script and prints it. See the open documentation page for more information.