Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make: Capture output of shell command and return code at same time

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?

like image 496
pythonic metaphor Avatar asked Jul 09 '14 15:07

pythonic metaphor


1 Answers

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 $$?)
like image 159
Alex Cohn Avatar answered Sep 24 '22 04:09

Alex Cohn