【发布时间】:2014-08-16 12:58:48
【问题描述】:
刚开始在这里使用 Perl。我之前在Perl中成功使用反引号捕获系统命令输出,如:
my @sysOut = `cleartool checkout -nc \"$file\"`; # works fine!
但是我遇到了一些麻烦,即使环顾了一段时间,我也没有找到解决这个问题的方法。我正在尝试编写一个 Perl 脚本以使用 cleartool 签入签出文件列表(@allfiles),除非有任何文件与其前身相同,然后取消签出它们。
我检测它们是否相同的方式(......失败!)是从检查尝试中获取输出,查看它是否匹配/error.*identical/i,然后如果匹配则取消签出文件。但是,由于某种原因,输出似乎绕过了我将其传递到的数组。
查看产生此问题的代码:
foreach my $file (@allfiles){
chomp( my @checkInErr = `cleartool checkin -nc \"$file\"`);
foreach my $err (@checkInErr) { # if no error, checkin done
if ($err =~ m/error.*identical/i) { # if there is error:
print $err;
print "No change detected: unchecking out.\n"; # uncheckout same version
system "cleartool uncheckout -rm -cact \"$file\"";
}
}
}
这是我的命令行输出(好像我刚刚使用了 system() 调用):
cleartool: Error: Unable to check in "a5TI.txt".
cleartool: Error: By default, won't create version with data identical to predecessor.
cleartool: Error: Unable to check in "a6cm.txt".
cleartool: Error: By default, won't create version with data identical to predecessor.
cleartool: Error: Unable to check in "a6FT.txt".
cleartool: Error: By default, won't create version with data identical to predecessor.
cleartool: Error: Unable to check in "a6pm.txt".
cleartool: Error: By default, won't create version with data identical to predecessor.
cleartool: Error: Unable to check in "a6TI.txt".
cleartool: Error: By default, won't create version with data identical to predecessor.
解决方案:在获取输出时也要检查 std 错误流(回想起来,这是有道理的,因为我试图解析错误消息......哦,好吧)
my @checkInErr = `cleartool checkin -nc \"$file\" 2>&1`;
【问题讨论】:
标签: perl command system output