2012-07-17 90 views
21

我需要运行与系统()在Perl外壳命令的输出。例如,捕获的Perl系统()

system('ls')

系统调用将打印到标准输出,但我想输出捕捉到一个变量,这样我可以做进一步处理我的Perl代码。

+1

不要使用系统,请使用open()和管道。 – 2012-07-17 01:41:42

+0

你能举个例子吗?谢谢! – Dagang 2012-07-17 01:42:20

+1

'perldoc -f open' – 2012-07-17 02:27:57

回答

32

这就是反引号是。从perldoc perlfaq8

为什么我不能得到一个命令与system()输出?

你混淆的system()和反引号(``)的目的。 system() 运行一个命令并返回退出状态信息(作为16位值: ,低7位是进程死亡信号,如果有的话,高8位是实际的退出值)。反引号(``)运行命令 并返回它发送到标准输出。

my $exit_status = system("mail-users"); 
my $output_string = `ls`; 

详情请参阅perldoc perlop

12

IPC::Run是我最喜欢这样的任务模块。非常强大和灵活,对于小型案例也非常简单。

use IPC::Run 'run'; 

run [ "command", "arguments", "here" ], ">", \my $stdout; 

# Now $stdout contains output 
+3

I我们一直在寻找一种简单的方法来通过命令参数而不使用shell插值,并从所谓的命令中捕获stdout。优秀的答案,谢谢! – 2013-12-30 10:59:01

3

只需使用类似来砸例如:

$variable=`some_command some args`; 

多数民众赞成。 通知,你会不会在输出看到任何印刷到标准输出,因为这会被重定向到变量。 除了您已准备好答案之外,此示例对与用户交互的命令不可用。对,你可以使用类似这样使用shell命令的堆栈:

$variable=`cat answers.txt|some_command some args`; 

answers.txt文件中,你应该准备回答所有的some_command正常工作。

我知道这不是编程的最佳方式:)但这是如何实现目标的最简单方法,特别适用于bash程序员。

当然,如果输出较大(ls与子目录),你不应该一次获得所有的输出。通过同样的方式读取命令,当你阅读regullar文件:

open CMD,'-|','your_command some args' or die [email protected]; 
my $line; 
while (defined($line=<CMD>)) { 
    print $line; #or push @table,$line or do whatewer what you want processing line by line 
} 
close CMD; 

附加处理长命令输出,无需额外的bash调用扩展的解决方案:

my @CommandCall=qw(find/-type d); #some example single command 
my $commandSTDOUT; #file handler 
my $pid=open($commandSTDOUT),'-|'); #there will be implict fork! 
if ($pid) { 
    #parent side 
    my $singleLine; 
    while(defined($singleline=<$commandSTDOUT>)) { 
     chomp $line; #typically we don't need EOL 
     do_some_processing_with($line); 
    }; 
    close $commandSTDOUT; #in this place $? will be set for capture 
    $exitcode=$? >> 8; 
    do_something_with_exit_code($exitcode); 
} else { 
    #child side, there you really calls a command 
    open STDERR, '>>&', 'STDOUT'; #redirect stderr to stdout if needed, it works only for child, remember about fork 
    exec(@CommandCall); #at this point child code is overloaded by external command with parameters 
    die "Cannot call @CommandCall"; #error procedure if call will fail 
} 

如果使用过程那样,你将捕获所有程序输出,并且您可以逐行执行所有处理。祝你好运:)

1

我想运行system()而不是backtacks,因为我想看到rsync --progress的输出。但是,我还想捕获输出以防出现错误,具体取决于返回值。 (这是用于备份脚本)。这是我现在使用的:

use File::Temp qw(tempfile); 
use Term::ANSIColor qw(colored colorstrip); 



sub mysystem { 
    my $cmd = shift; #"rsync -avz --progress -h $fullfile $copyfile"; 
    my ($fh, $filename) = tempfile(); 
    # http://stackoverflow.com/a/6872163/2923406 
    # I want to have rsync progress output on the terminal AND capture it in case of error. 
    # Need to use pipefail because 'tee' would be the last cmd otherwise and hence $? would be wrong. 
    my @cmd = ("bash", "-c", "set -o pipefail && $cmd 2>&1 | tee $filename"); 
    my $ret = system(@cmd); 
    my $outerr = join('', <$fh>); 
    if ($ret != 0) { 
     logit(colored("ERROR: Could not execute command: $cmd", "red")); 
     logit(colored("ERROR: stdout+stderr = $outerr", "red")); 
     logit(colored("ERROR: \$? = $?, \$! = $!", "red")); 
    } 
    close $fh; 
    system("rm $filename"); 
    return $ret; 
} 

# and logit() is sth like: 
sub logit { 
    my $s = shift; 
    my ($logsec,$logmin,$loghour,$logmday,$logmon,$logyear,$logwday,$logyday,$logisdst)=localtime(time); 
    $logyear += 1900; 
    my $logtimestamp = sprintf("%4d-%02d-%02d %02d:%02d:%02d",$logyear,$logmon+1,$logmday,$loghour,$logmin,$logsec); 
    my $msg = "$logtimestamp $s\n"; 
    print $msg; 
    open LOG, ">>$LOGFILE"; 
    print LOG colorstrip($msg); 
    close LOG; 
} 
+0

你为什么要调用system(“rm $ filename”)?你有内部程序取消链接$ filename,它没有额外的调用sh或bash解释器。 – Znik 2018-02-02 12:38:35