2009-09-25 117 views
20

我想输出写入使用Perl创建的文件的Windows命令行程序(例如,powercfg -l),然后在for循环中逐行读取文件并将其分配给字符串。如何在Perl中获得外部命令的输出?

+0

请更新您的问题,并在答案的评论中提供所有详细信息,以便人们可以更好地为您提供帮助。您收到的错误消息以及您正在使用的示例将有所帮助。 – 2009-09-26 17:14:03

回答

20
my $output = qx(powercfg -l); 

## You've got your output loaded into the $output variable. 
## Still want to write it to a file? 
open my $OUTPUT, '>', 'output.txt' or die "Couldn't open output.txt: $!\n"; 
print $OUTPUT $output; 
close $OUTPUT 

## Now you can loop through each line and 
## parse the $line variable to extract the info you are looking for. 
foreach my $line (split /[\r\n]+/, $output) { 
    ## Regular expression magic to grab what you want 
} 
22
system 'powercfg', '-l'; 

是推荐的方法。如果你不介意产生子壳,

system "powercfg -l"; 

也可以。如果你想在一个字符串的结果:

my $str = `powercfg -l`; 
+0

比我的回答更好 – Xetius 2009-09-16 14:35:24

+1

不要忘记检查系统调用的返回值。 – Craig 2009-09-16 14:36:30

+0

@craig:有没有办法检查返回值并捕获字符串中的输出? – jimtut 2009-09-16 16:15:14

2

由于OP运行powercfg,他/她可能正在捕捉外部脚本的输出中,那么他/她可能会找不到这个答案非常有用。这篇文章主要是为通过搜索找到答案的其他人撰写的。

此答案描述了几种启动将在后台运行的命令的方法,而不会阻止脚本的进一步执行。

看看perlport entry for system。你可以使用system(1, 'command line to run');产生一个子进程并继续你的脚本。

这真的很方便,但有一个严重的警告是不是记录。如果在脚本的一次执行中启动更多64个进程,则尝试运行其他程序将失败。

我已经验证这是Windows XP和ActivePerl 5.6和5.8的情况。我还没有用Vista或Stawberry Perl或5.10的任何版本进行测试。

这里有一个衬垫,你可以用它来测试你的Perl对于这个问题:

C:\>perl -e "for (1..100) { print qq'\n $_\n-------\n'; system(1, 'echo worked'), sleep 1 } 

如果你的系统中存在的问题,你会开始很多程序,你可以使用Win32::Process模块管理你的程序启动。

下面是一个使用Win32::Process的例子:

use strict; 
use warnings; 

use Win32::Process; 

if(my $pid = start_foo_bar_baz()) { 
    print "Started with $pid"; 
} 
:w 

sub start_foo_bar_baz { 

    my $process_object; # Call to Create will populate this value. 
    my $command = 'C:/foo/bar/baz.exe'; # FULL PATH to executable. 
    my $command_line = join ' ', 
      'baz', # Name of executable as would appear on command line 
      'arg1', # other args 
      'arg2'; 

    # iflags - controls whether process will inherit parent handles, console, etc. 
    my $inherit_flags = DETACHED_PROCESS; 

    # cflags - Process creation flags. 
    my $creation_flags = NORMAL_PRIORITY_CLASS; 

    # Path of process working directory 
    my $working_directory = 'C:/Foo/Bar'; 

    my $ok = Win32::Process::Create(
     $process_object, 
     $command, 
     $command_line, 
     $inherit_flags, 
     $creation_flags, 
     $working_directory, 
    ); 

    my $pid; 
    if ($ok) { 
     $pid = $wpc->GetProcessID; 
    } 
    else { 
     warn "Unable to create process: " 
      . Win32::FormatMessage(Win32::GetLastError()) 
     ; 
     return; 
    } 

    return $pid; 
} 
1

尝试使用>运算符输出转发到一个文件,如:

powercfg -l > output.txt 

,然后打开output.txt中,并对其进行处理。

+0

您好我想在此....中使用Perl,因为我不知道如何捕获文件中的输出(要创建)并逐行读取该文件并将该行分配给PERL脚本中的字符串。 – 2009-09-25 14:16:30

8

没有必要先保存命令的输出文件:

my $output = `powercfg -l`; 

qx//Quote-Like Operators

但是,如果你想先保存在一个文件输出,那么你可以使用:

my $output_file = 'output.txt'; 

system "powercfg -l > $output_file"; 

open my $fh, '<', $output_file 
    or die "Cannot open '$output_file' for reading: $!"; 

while (my $line = <$fh>) { 
    # process lines 
} 

close $fh; 

perldoc -f system

2

要扩大思南的出色答卷,并更明确地回答你的问题:

注:反引号``告诉Perl来执行命令和检索其输出:

#!/usr/bin/perl -w 
use strict; 


my @output = `powercfg -l`; 
chomp(@output); # removes newlines 

my $linecounter = 0;  
my $combined_line; 

foreach my $line(@output){ 
    print $linecounter++.")"; 
    print $line."\n"; #prints line by line 

    $combined_line .= $line; # build a single string with all lines 
    # The line above is the same as writing: 
    # $combined_line = $combined_line.$line; 
} 

print "\n"; 
print "This is all on one line:\n"; 
print ">".$combined_line."<"; 

你的输出(在我的系统上)应该是:

0) 
1)Existing Power Schemes (* Active) 
2)----------------------------------- 
3)Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced) * 
4)Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (High performance) 
5)Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a (Power saver) 

This is all on one line: 
>Existing Power Schemes (* Active)-----------------------------------Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced) *Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (High performance)Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a (Power saver)< 

Perl让它变得简单!

19

你已经有了一些很好的答案。另外,如果您只想处理命令的输出,而不需要直接将该输出发送到文件,则可以在命令和Perl脚本之间建立管道。

use strict; 
use warnings; 

open(my $fh, '-|', 'powercfg -l') or die $!; 
while (my $line = <$fh>) { 
    # Do stuff with each $line. 
} 
+3

从while语句中的文件句柄读取时,不需要定义的测试。查看'perl -MO = Deparse -e“的输出结果,同时<>”' – 2009-09-25 15:23:08

+0

@Sinan非常感谢。我不熟悉'-MO = Deparse'。很有用。 – FMc 2009-09-25 15:56:14

+0

这很好,因为如果命令恰好会产生大量的输出并且您开始交换,那么我的$ var ='command'可能会失控。不典型,但它发生。 – 2014-12-17 18:33:35

相关问题