5

In make, if I want to capture the output of a shell command, I do something like this

RESULT:=$(shell $(COMMAND))

If I want to check if a command executed properly, I do this

RETURN_CODE := $(shell $(COMMAND); echo $$?)

How can I do both simultaneously, i.e. execute the command once, store the output, but also check the return code?

EDIT Duplicate here although his solution is not pleasant: Makefile: Output and Exitcode to variable?

Community
  • 1
  • 1
pythonic metaphor
  • 10,296
  • 18
  • 68
  • 110

1 Answers1

6

What about

OUTPUT_WITH_RC := $(shell $(COMMAND); echo $$?)
RETURN_CODE := $(lastword $(OUTPUT_WITH_RC))
OUTPUT := $(subst $(RETURN_CODE)QQQQ,,$(OUTPUT_WITH_RC)QQQQ)

If your command fails, it will probably write to stderr; you can use this to capture everything:

OUTPUT_WITH_RC := $(shell $(COMMAND) 2>$1; echo $$?)
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307