我正在运行一个退出代码为0或1表示成功的程序。我在构建docker映像时正在运行此程序,因此如果返回码不为0,则构建失败。如果实际退出代码为0或1,如何捕获退出代码并强制退出代码为0,以便可以正确构建docker映像?
我已经尝试过类似的操作,其中(出口1)代表程序:
((exit 1) && if [ $? == 0 || $? == 1]; then exit 0; else exit 1; fi;)
但是它不起作用,退出代码1仍然会以1退出。
如果程序由于某种原因实际失败,我宁愿不执行program || true
谢谢!
如果您的程序以代码0退出,则无需检查任何内容。但是,您想要的是将代码1“转换”为代码0。
这应该起作用:
((exit 0) || if [ $? == 1 ]; then exit 0; else exit 1; fi)
一些测试要检查(我更改了退出代码值以更好地了解正在发生的事情:]]
$> ((exit 0) || if [ $? == 1 ]; then exit 2; else exit 3; fi); echo $?
0 # the right-part of the condition is ignored
$> ((exit 1) || if [ $? == 1 ]; then exit 2; else exit 3; fi); echo $?
2 # the exit code 1 is "converted" into an exit code 2 (or 0 in your case)
$> ((exit 2) || if [ $? == 1 ]; then exit 2; else exit 3; fi); echo $?
3 # the exit code 2 is "converted" into an exit code 3 (or 1 in your case)