简体   繁体   English

在附加到tee的重定向中获取嵌套命令的退出状态

[英]Getting the exit status of a nested command inside a redirect attached to tee

I have this command: 我有以下命令:

coverage report | tee >(grep TOTAL | awk '{exit (int($4) >= 75)?0:1}') && (exit ${PIPESTATUS[0]})

which reports the code coverage and then prints the output to stdout and redirects that same output to a grep + awk, which basically checks whether the code coverage covers less or more than 75 . 它报告代码覆盖率,然后将输出打印到stdout ,并将该输出重定向到grep + awk,后者基本上检查代码覆盖率是否小于或大于75 Finally, there is an exit in a subshell, which I pretend to use in my CI/CD pipeline. 最后,在子外壳中有一个exit ,我假装在我的CI / CD管道中使用。

The problem is that my CI/CD always completes successfully, which shouldn't happen as the code coverage is less than 75% in my tests. 问题是我的CI / CD总是能够成功完成,这不会发生,因为在我的测试中代码覆盖率小于75%。 That means that the PIEPSTATUS isn't returning what I'm expecting it to return (awk's exit code). 这意味着PIEPSTATUS不会返回我期望它返回的内容(awk的exit代码)。

Where is the problem? 问题出在哪儿? What am I doing wrong? 我究竟做错了什么?

Exit status of 的退出状态

command | tee >(...)

will be the exit status of tee , regardless of what happens inside the process substitution >(...) . 不论进程替换 >(...)内部发生什么,都将是tee的退出状态。 That's the reason your code is not working. 这就是您的代码无法正常工作的原因。

You can achieve your goal without having to use process substitution, like this: 您无需使用流程替代即可实现目标,如下所示:

coverage report | awk '{print} /TOTAL/ { pcnt=$4 } END {exit (int(pcnt) >= 75) ? 0 : 1}')
  • {print} prints all the lines {print}打印所有行
  • /TOTAL/ ... grabs the percentage and saves it in pcnt /TOTAL/ ...获取百分比并将其保存在pcnt
  • END ... exits code based on pcnt END ...退出基于pcnt代码

This will not only print all the lines emitted by coverage report , it would also make sure that the exit code of pipeline reflects the coverage percentage logic. 这不仅将打印coverage report发出的所有行,还将确保管道的退出代码反映覆盖率百分比逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM