回答
这就是反引号是。从perldoc perlfaq8
:
为什么我不能得到一个命令与
system()
输出?你混淆的
system()
和反引号(``)的目的。system()
运行一个命令并返回退出状态信息(作为16位值: ,低7位是进程死亡信号,如果有的话,高8位是实际的退出值)。反引号(``)运行命令 并返回它发送到标准输出。my $exit_status = system("mail-users"); my $output_string = `ls`;
详情请参阅perldoc perlop
。
IPC::Run
是我最喜欢这样的任务模块。非常强大和灵活,对于小型案例也非常简单。
use IPC::Run 'run';
run [ "command", "arguments", "here" ], ">", \my $stdout;
# Now $stdout contains output
I我们一直在寻找一种简单的方法来通过命令参数而不使用shell插值,并从所谓的命令中捕获stdout。优秀的答案,谢谢! – 2013-12-30 10:59:01
只需使用类似来砸例如:
$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
}
如果使用过程那样,你将捕获所有程序输出,并且您可以逐行执行所有处理。祝你好运:)
我想运行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;
}
你为什么要调用system(“rm $ filename”)?你有内部程序取消链接$ filename,它没有额外的调用sh或bash解释器。 – Znik 2018-02-02 12:38:35
- 1. 如何从Perl脚本捕获系统命令的stdin和stdout?
- 2. 在Clojure捕获系统/出
- 3. 捕获系统信息
- 4. perl中的系统()
- 5. 如何捕获系统的输出()
- 6. 系统日期捕获到数据库
- 7. 使用python捕获系统事件
- 8. Joomla - 如何捕获系统事件
- 9. Wireshark远程系统数据包捕获
- 10. Perl的排队系统
- 11. oracle perl和perl系统并排安装
- 12. 发布系统在Perl
- 13. 系统调用在Perl
- 14. Perl系统+分割+数组
- 15. Perl,SAX和系统实体
- 16. perl系统命令执行
- 17. Perl错误捕获变量
- 18. Perl命名捕获组
- 19. Perl DBI - 捕获错误
- 20. 系统(perl的-pi -e)的XML编辑
- 21. 角2 - 未捕获的(以诺)的ReferenceError:系统没有定义
- 22. 在Windows上的Perl中获取当前系统本地编码
- 23. Perl:与STDOUT分开获取系统调用的STDERR
- 24. Perl的 '系统' 失败消息
- 25. 系统命令VS Perl的子程序
- 26. perl“系统”脚本的问题
- 27. Perl的图案匹配系统变量
- 28. 获取系统
- 29. 未捕获的ReferenceError:系统未定义在(索引) - 角2
- 30. 系统托盘是否有可以捕获的事件?
不要使用系统,请使用open()和管道。 – 2012-07-17 01:41:42
你能举个例子吗?谢谢! – Dagang 2012-07-17 01:42:20
'perldoc -f open' – 2012-07-17 02:27:57