2

I am searching for a way to get the output and the exit code of a command to variables in a makefile.

Basicly I want this: (bash)

output=$(whoami)
returnval=$?
echo "OUT:"
echo $output
echo "RET:"
echo $returnval

to be in a makefile

note: should work in a rule section

Thanks

EDIT: SOLVED

$(eval OUTPUT_RC="$(shell whoami;echo $$?)")
$(eval OUTPUT="$(shell echo $(OUTPUT_RC) | sed -e "s/^\(.*\)\(.\{2\}\)$$/\1/")")
$(eval RC="$(shell echo $(OUTPUT_RC) | sed -e "s/^.*\(.\)$$/\1/")")
echo $(OUTPUT)
echo $(RC)
Lars Blumberg
  • 16,724
  • 10
  • 81
  • 114
Eun
  • 4,056
  • 3
  • 28
  • 49
  • Another solution that is also complicated but uses less external processes can be found here: http://stackoverflow.com/a/40710111/1905491 You don't need the `eval`s there btw. Assinging with `:=` should suffice. – stefanct Nov 20 '16 at 23:03
  • Using a shell to split the combined result is a lot more awful than just running two separate commands. Why would you ever expect `whoami` to return an error? – tripleee Dec 12 '17 at 08:43
  • 1
    `whoami` is just an example imagine a compiler command, you do not want to run a compiler command two times. – Eun Dec 12 '17 at 08:46

2 Answers2

0

If you are going to use eval anyway, make it print things you can eval directly.

$(eval $(shell echo OUTPUT_RC=`whoami`; echo $$?))
OUTPUT=$(word 1,$(OUTPUT_RC))
RC=$(word 2,$(OUTPUT_RC))
tripleee
  • 158,107
  • 27
  • 234
  • 292
-1

GNU make offers the shell function, as in:

OUTPUT=$(shell whoami)
OUTPUT_RC=$(shell whoami; echo $$?)
thiton
  • 35,082
  • 3
  • 67
  • 98
  • 1
    the bad thing about this is that it runs 2 times, imagin it is an compile command – Eun Jun 22 '12 at 13:57