从 Perl 调用命令,需要查看输出

发布于 2024-10-07 18:35:58 字数 214 浏览 0 评论 0原文

我需要从 perl 调用一些 shell 命令。这些命令需要相当长的时间才能完成,因此我想在等待完成时查看它们的输出。

system 函数在完成之前不会给我任何输出。

exec 函数给出输出;但是,它从那时起退出 perl 脚本,这不是我想要的。

我在 Windows 上。有办法做到这一点吗?

I need to call some shell commands from perl. Those commands take quite some time to finish so I'd like to see their output while waiting for completion.

The system function does not give me any output until it is completed.

The exec function gives output; however, it exits the perl script from that point, which is not what I wanted.

I am on Windows. Is there a way to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

瑕疵 2024-10-14 18:35:58

反引号,或 qx 命令,在单独的进程并返回输出:

print `$command`;
print qx($command);

如果您希望查看中间输出,请使用 open 创建命令输出流的句柄并从中读取。

open my $cmd_fh, "$command |";   # <---  | at end means to make command 
                                 #         output available to the handle
while (<$cmd_fh>) {
    print "A line of output from the command is: $_";
}
close $cmd_fh;

Backticks, or the qx command, run a command in a separate process and returns the output:

print `$command`;
print qx($command);

If you wish to see intermediate output, use open to create a handle to the command's output stream and read from it.

open my $cmd_fh, "$command |";   # <---  | at end means to make command 
                                 #         output available to the handle
while (<$cmd_fh>) {
    print "A line of output from the command is: $_";
}
close $cmd_fh;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文