Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check return value from the shell directive

In my Makefile, I need to test if the current directory is an SVN repo or not and if it is not I want to indicate an error using the $(error) directive in Makefile.

So I plan to use the return value of $(shell svn info .) but I'm not sure how to get this value from within the Makefile.

Note: I'm not trying to get the return value in a recipe, but rather in the middle of the Makefile.

Right now I'm doing something like this, which works just because stdout is blank when it is an error:

SVN_INFO := $(shell svn info . 2> /dev/null) ifeq ($(SVN_INFO),)     $(error "Not an SVN repo...") endif 

I'd still like to find out if it is possible to get the return value instead within the Makefile.

like image 434
Tuxdude Avatar asked Sep 12 '11 21:09

Tuxdude


People also ask

How do you find the return value of a command?

The return value of a command is stored in the $? variable. The return value is called exit status. This value can be used to determine whether a command completed successfully or unsuccessfully.

What is the command to check return status in Linux shell?

“$?” is a variable that holds the return value of the last executed command. “echo $?” displays 0 if the last command has been successfully executed and displays a non-zero value if some error has occurred. The bash sets “$?” To the exit status of the last executed process.

What is the return value of a shell script after successful execution?

Every command returns an exit status (sometimes referred to as a return status or exit code). A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code.


2 Answers

How about using $? to echo the exit status of the last command?

 SVN_INFO := $(shell svn info . 2> /dev/null; echo $$?) ifeq ($(SVN_INFO),1)     $(error "Not an SVN repo...") endif 
like image 63
eriktous Avatar answered Sep 22 '22 20:09

eriktous


If you want to preserve the original output then you need to do some tricks. If you are lucky enough to have GNU Make 4.2 (released on 2016-05-22) or later at your disposal you can use the .SHELLSTATUS variable as follows.

var := $(shell echo "blabla" ; false)  ifneq ($(.SHELLSTATUS),0)   $(error shell command failed! output was $(var)) endif  all:     @echo Never reached but output would have been $(var) 

Alternatively you could use a temporary file or play with Make's eval to store the string and/or the exit code into a Make variable. The example below gets this done but I would certainly like to see a better implementation than this embarrassingly complicated version.

ret := $(shell echo "blabla"; false; echo " $$?") rc := $(lastword $(ret)) # Remove the last word by calculating <word count - 1> and # using it as the second parameter of wordlist. string:=$(wordlist 1,$(shell echo $$(($(words $(ret))-1))),$(ret))  ifneq ($(rc),0)   $(error shell command failed with $(rc)! output was "$(string)") endif  all:     @echo Never reached but output would have been \"$(string)\" 
like image 43
stefanct Avatar answered Sep 23 '22 20:09

stefanct