Makefile:执行多个命令,忽略错误并返回退出代码

huangapple go评论51阅读模式
英文:

Makefile: Execute several commands, ignore errors and return exit code

问题

I have a Makefile in which I would like to run several commands and ignore errors. However, the make target will fail if one of the commands fails. I am running this Makefile on Mac, Ubuntu, and Redhat, so the versions of make are different. I believe the lowest version is 3.8.

Currently, my solution is:

target:
    RC1=0; RC2=0; RC3=0; \
	command1; \
	RC1=$$?; \
	command1=2; \
	RC2=$$?; \
	command3; \
	RC3=$$?; \
	if [ $$RC1 -ne 0 ] || [ $$RC2 -ne 0 ] || [ $$R3 -ne 0 ]; then \
		  RC=1; \
	fi; \
	exit $$RC

The problem is at the end with the if statement: Later, I might insert more commands, command4, command5, ... and the if statement will be very clunky. Is there a simpler solution?

I wish if I can do something like this:

target:
	-command1
	-command2
	-command3
	# Exit with a non-zero code if one of the commands failed
英文:

I have a Makefile in which I would like to run several commands and ignore error. However, the make target will fail if one of the commands fail. I am running this Makefile on Mac, Ubuntu, and Redhat, so the versions of make are different. I believe the lowest version is 3.8.

Currently, my solution is:

target:
    RC1=0; RC2=0; RC3=0; \
	command1; \
	RC1=$$?; \
	command1=2; \
	RC2=$$?; \
	command3; \
	RC3=$$?; \
	if [ $$RC1 -ne 0 ] || [ $$RC2 -ne 0 ] || [ $$R3 -ne 0 ]; then \
		  RC=1; \
	fi; \
	exit $$RC

The problem is at the end with the if statement: Later, I might insert more commands, command4, command5, ... and the if statement will be very clunkly. Is there a simpler solution?

I wish if I can do something like this:

target:
	-command1
	-command2
	-command3
	# Exit with non-zero code if one of the command failed

答案1

得分: 1

我不认为保留所有这些单独的错误代码有任何意义。为什么不只使用:

target:
        RC=0; \
        command1 || RC=1; \
        command1 || RC=1; \
        command3 || RC=1; \
        exit $$RC

我想不出比这更简单的方法了。

英文:

I don't see any point in keeping all these individual error codes. Why not just use:

target:
        RC=0; \
        command1 || RC=1; \
        command1 || RC=1; \
        command3 || RC=1; \
        exit $$RC

I can't think of a simpler way than that though.

huangapple
  • 本文由 发表于 2023年6月5日 00:08:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定