如何隐藏在sytem()内运行的命令的输出?

时间:2017-01-16 01:55:45

标签: perl

我看了很多,发现了很多答案,经过多次试验和错误,我得出了这样的结论:我不知道。

我在Perl中运行一个东西:

my $command = sprintf('commandHereDoingSCPishThingsThatHasToRunInMyShell', options, options, options);
system($command);

当我在终端窗口中运行脚本时,我在sprintf()函数中看到了命令的输出。

我希望将输出重定向到文件,而不必在终端窗口上看到程序正在执行。

谢谢!

1 个答案:

答案 0 :(得分:2)

不鼓励使用perl的system(),因为检测错误并提出相关的错误消息并非易事。考虑使用IPC::System::Simple。由于您想捕获输出,请使用其capture()。

use IPC::System::Simple 'capture';

my $output = capture($command);  # dies if command fails; catch this with eval if necessary
File::Slurp::write_file('somefile', $output);

或者,只需将输出重定向到命令中的文件:

use IPC::System::Simple 'system';
system("$command >somefile");