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

标签 perl shellexecute

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

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

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

我使用的是 Windows。有办法实现吗?

最佳答案

Backticksqx 命令,在单独的进程中运行命令并返回输出:

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;

关于perl - 从 Perl 调用命令,需要查看输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4443659/

相关文章:

C++,ShellExecute a URL with the hash sign (#)

c++ - 如何以编程方式打开 Run c++?

perl - AA 和丹麦校对有什么关系?

几次快速插入后,MySQL 插入变慢

perl - 如何按插入顺序将哈希值提取到数组中?

database - 如何使用 perl 将数据库的每一行保存为目录中的单独数据 block 文件?

delphi - 不兼容的类型 pwidechar 和 string ShellExecute

VBA:运行 "Elevated"命令(Shell 与 ShellExecute)

perl - 在mod_perl中通过Perl中的 `use`和 `require`语句来搜索具有当前源文件的目录的规范方法是什么?

windows - 确定已注册的延期申请