【问题标题】:problem while capturing the error of `make`捕获“make”错误时出现问题
【发布时间】:2011-08-04 08:42:18
【问题描述】:

这个 perl 脚本背后的目的是首先重构一个.cpp 文件,然后编译整个包。如果一切顺利,则继续下一个文件,否则替换 backup 目录中的原始文件,依此类推。以下是运行包的makefile 的perl 脚本。

@lcpp = `ls *.cpp`; 
chomp(@lcpp);
foreach (@lcpp) {
print "processing file $_ ...";
`cp demac_dir/$_ .`;
if(2 == `make`) {
  print "\n\t\t\tError in the file\n";
  `cp backup/$_ .`;
  print "reverting back to the original file and building the package again";
  `make`;
}
else {#when successfully compiled
  print "successfully compiled the package with file $_";
}
}

脚本一直运行,直到我得到一个带有编译器错误的“重构”文件。我猜该脚本无法捕获make 返回的错误。还是我错过了什么。

【问题讨论】:

  • 为什么要反引号而不是直接调用system
  • 好的……我正在尝试 system()
  • 虽然有这么多的系统调用,但似乎使用 perl 是多余的。将第一行与my @lcpp = glob "*.cpp"; 交换,然后您将获得一个非常平滑的数组,而无需对其进行咀嚼。 File::Copy 用于复制/移动命令。我怀疑 make 也有一些不错的东西。

标签: perl makefile


【解决方案1】:

几乎可以肯定会使错误转到 STDERR,这不会被反引号捕获。使用Capture::Tiny 轻松捕获两个输出流。

【讨论】:

  • 我只是希望如果 make 不成功,那么脚本应该“知道”并做必要的事情。是的,您是对的,错误消息转到“stderr”
  • @Aditya Kumar - 我在想您想解析输出以查看发生了什么错误。如果您只需要成功,检查$? 可能是最快的方法。
【解决方案2】:

如果你使用system()调用make,你可以检查make是否成功。见perldoc -f system:

@args = ("命令", "arg1", "arg2"); 系统(@args)== 0 或死“系统@args失败:$?” 您可以通过检查 $? 来检查所有失败的可能性? 像这样: 如果 ($? == -1) { print "执行失败:$!\n"; } elif ($? & 127) { printf "孩子死于信号 %d, %s coredump\n", ($? & 127), ($? & 128) ? '有没有'; } 别的 { printf "子退出,值为 %d\n", $? >> 8; }

【讨论】:

  • 现在我正在尝试 system()... 让我们看看结果如何...感谢您的建议
  • 是的 system() 函数运行良好...我应该在 3-4 小时前问...谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
  • 2012-07-28
  • 2014-07-22
  • 2021-05-05
  • 1970-01-01
  • 2018-01-15
  • 1970-01-01
相关资源
最近更新 更多