我如何在Perl的外部命令的输出?(How do I get the output of an ex

2019-07-19 15:28发布

我想有Windows命令行程序的输出(比如, powercfg -l )写入作为使用Perl创建,然后逐行读取文件中的行中的for循环,并将其分配给一个字符串的文件。

Answer 1:

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
}


Answer 2:

你有一些好的答案了。 此外,如果你只是想处理一个命令的输出,不需要到输出直接发送到一个文件,你可以建立命令和你的Perl脚本之间的管道。

use strict;
use warnings;

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


Answer 3:

system 'powercfg', '-l';

是推荐的方式。 如果你不介意产卵子shell,

system "powercfg -l";

会工作,太。 如果你想在一个字符串的结果:

my $str = `powercfg -l`;


Answer 4:

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

my $output = `powercfg -l`;

qx//在报价样运营商 。

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

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系统 。



Answer 5:

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

这个答案介绍了几种方法来启动会在后台运行,不会阻塞你的脚本的进一步执行命令。

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

这真的是很方便,但有一个没有文档一个严重的警告。 如果您在脚本的一个开始执行64多个流程,您尝试运行其他程序将失败。

我已经验证这是与Windows XP和的ActivePerl 5.6和5.8的情况下。 我还没有与Vista或与草莓Perl或5.10任何版本测试这一点。

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

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

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

下面是使用的例子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;
}


Answer 6:

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

注:反引号``告诉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中可以很容易!



Answer 7:

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

powercfg -l > output.txt

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



文章来源: How do I get the output of an external command in Perl?