我想输出写入使用Perl创建的文件的Windows命令行程序(例如,powercfg -l
),然后在for循环中逐行读取文件并将其分配给字符串。如何在Perl中获得外部命令的输出?
回答
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
}
由于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;
}
尝试使用>运算符输出转发到一个文件,如:
powercfg -l > output.txt
,然后打开output.txt中,并对其进行处理。
您好我想在此....中使用Perl,因为我不知道如何捕获文件中的输出(要创建)并逐行读取该文件并将该行分配给PERL脚本中的字符串。 – 2009-09-25 14:16:30
没有必要先保存命令的输出文件:
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;
要扩大思南的出色答卷,并更明确地回答你的问题:
注:反引号``告诉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让它变得简单!
你已经有了一些很好的答案。另外,如果您只想处理命令的输出,而不需要直接将该输出发送到文件,则可以在命令和Perl脚本之间建立管道。
use strict;
use warnings;
open(my $fh, '-|', 'powercfg -l') or die $!;
while (my $line = <$fh>) {
# Do stuff with each $line.
}
从while语句中的文件句柄读取时,不需要定义的测试。查看'perl -MO = Deparse -e“的输出结果,同时<>”' – 2009-09-25 15:23:08
@Sinan非常感谢。我不熟悉'-MO = Deparse'。很有用。 – FMc 2009-09-25 15:56:14
这很好,因为如果命令恰好会产生大量的输出并且您开始交换,那么我的$ var ='command'可能会失控。不典型,但它发生。 – 2014-12-17 18:33:35
- 1. 如何在Perl中实时读取外部命令的输出?
- 2. 如何在Perl中获得Win32 :: Process命令的输出?
- 3. 如何在Perl中运行外部命令并捕获其输出?
- 4. 如何从Perl脚本中的外部命令中收集输出?
- 5. 如何获得在命令窗口中出现的matlab输出
- 6. 如何执行外部命令并捕获输出?
- 7. 如何在Go中获得系统命令的输出?
- 8. 从外部* nix命令获得STDOUT,STDERR和响应代码perl
- 9. 如何在执行外部命令时在Perl中捕获较大的STDOUT输出
- 10. 如何在Mac OS中实时获得顶级命令输出
- 11. 如何在perl6中执行外部命令并捕获其输出?
- 12. 获得一个Perl命令输出到变量壳
- 13. 命令输出和perl输出不同
- 14. 为什么我不能在Perl中使用system()获得命令的输出?
- 15. 如何在命令类之外获得命令参数?
- 16. 在Perl中输出shell命令的输出
- 17. 如何从Perl管道获得输出?
- 18. Ruby:即使没有换行符,也能获得外部命令的输出
- 19. 如何从Perl的外部命令中丢弃STDERR?
- 20. 在Perl中调用命令行函数并获得输出为字符串
- 21. 得到命令的输出
- 22. “less”命令如何获得stdin输入?
- 23. 如何获得使用shell的命令输出的某些部分?
- 24. 如何使用gradle获得adb shell命令的输出?
- 25. 如何从外部命令结合管道输出
- 26. Perl外部命令执行错误
- 27. 如何获得蒙戈外壳输出(三点)对未终止命令
- 28. 得到输出的命令在ObjC
- 29. 如何从等待输入的命令中捕获命令输出?
- 30. 如何使用Python获得Maven输出(或命令行日志)?
请更新您的问题,并在答案的评论中提供所有详细信息,以便人们可以更好地为您提供帮助。您收到的错误消息以及您正在使用的示例将有所帮助。 – 2009-09-26 17:14:03