捕获Perl system()的输出 [英] Capture the output of Perl system()
问题描述
我需要在Perl中使用system()运行shell命令.例如,
I need to run a shell command with system() in Perl. For example,
system('ls')
系统调用将打印到STDOUT,但是我想将输出捕获到变量中,以便将来可以使用Perl代码进行处理.
The system call will print to STDOUT, but I want to capture the output into a variable so that I can do future processing with my Perl code.
推荐答案
这就是反引号的用途.来自 perldoc perlfaq8
:
That's what backticks are for. From perldoc perlfaq8
:
为什么不能使用
system()
来获取命令的输出?
Why can't I get the output of a command with
system()
?
您混淆了 system()
和反引号(``)的用途. system()
运行命令并返回退出状态信息(作为16位值:
低7位是进程死亡的信号(如果有),并且
高8位是实际退出值).反引号(``)运行命令
并返回发送到STDOUT的内容.
You're confusing the purpose of system()
and backticks (``). system()
runs a command and returns exit status information (as a 16 bit value:
the low 7 bits are the signal the process died from, if any, and the
high 8 bits are the actual exit value). Backticks (``) run a command
and return what it sent to STDOUT.
my $exit_status = system("mail-users");
my $output_string = `ls`;
有关更多详细信息,请参见 perldoc perlop
.
See perldoc perlop
for more details.
这篇关于捕获Perl system()的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!