【问题标题】:System command output leaking - perl系统命令输出泄漏 - perl
【发布时间】: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


    【解决方案1】:

    可能来自 cleartool 的错误输出没有出现在标准输出 (stdout) 上。运气好的话,它会出现在标准错误 (stderr) 上。如果是这样,这应该有效:

    system "cleartool uncheckout -rm -cact \"$file\" 2>&1";
    

    【讨论】:

    • 是的,我想就是这样!添加 stderr 使它正确解析,所以它一定已经去那里了。完美
    猜你喜欢
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    相关资源
    最近更新 更多