如何在 Perl 中刷新反引号中的输出?

发布于 2024-07-17 00:36:29 字数 201 浏览 7 评论 0原文

如果我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

靑春怀旧 2024-07-24 00:36:30

这里的问题是,使用反引号转义会将脚本存储到字符串中,然后打印该字符串。 因此,无法用打印来“刷新”。

使用 system() 命令应该连续打印输出,但您将无法捕获输出:

system "someshellscript.sh";

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:

system "someshellscript.sh";
血之狂魔 2024-07-24 00:36:29

您可能想要做的是这样的:

open(F, "someshellscript.sh|");
while (<F>) {
    print;
}
close(F);

这将运行 someshellscript.sh 并打开一个读取其输出的管道。 while 循环读取脚本生成的每一行输出并将其打印出来。 有关详细信息,请参阅 open 文档页面。

What you probably want to do is something like this:

open(F, "someshellscript.sh|");
while (<F>) {
    print;
}
close(F);

This runs someshellscript.sh and opens a pipe that reads its output. The while loop reads each line of output generated by the script and prints it. See the open documentation page for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文