Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell status codes in make

Tags:

shell

makefile

I use a Makefile (with GNU make running under Linux) to automate my grunt work when refactoring a Python script. The script creates an output file, and I want to make sure that the output file remains unchanged in face of my refactorings.

However, I found no way to get the status code of a command to affect a subsequent shell if command.

The following rule illustrates the problem:

check-cond-codes:
    diff report2008_4.csv report2008_4.csv-save-for-regression-testing; echo no differences: =$$!=
    diff -q poalim report2008_4.csv; echo differences: =$$!=

The first 'diff' compares two equal files, and the second one compares two different files. The output is:

diff report2008_4.csv report2008_4.csv-save-for-regression-testing; echo no differences: =$!=
no differences: ==
diff -q poalim report2008_4.csv; echo differences: =$!=
Files poalim and report2008_4.csv differ
differences: ==

So obviously '$$!' is the wrong variable to capture the status code of 'diff'. Even using SHELL := /bin/bash at beginning of the Makefile did not solve the problem.

A variable returning the value, which I need, would (if it exists at all) be used in an 'if' command in the real rule.

The alternative of creating a small ad-hoc shell script in lieu of writing all commands inline in the Makefile is undesirable, but I'll use it as a last resort.

Related:

  • How to make a failing shell command interrupt make
like image 606
Omer Zak Avatar asked Apr 14 '09 16:04

Omer Zak


People also ask

What is shell in Makefile?

$(shell) is a special function in gmake that runs an external command and captures the output for use in the makefile. For example, you could get the current working directory like this: CWD=$(shell pwd) all: @echo This makefile lives in $(CWD).

What does 255 exit code mean?

Depending on our shell, exit code 255 might mean that the returned exit code is outside of the 0-255 range.

What is exit 10 in shell script?

Exit Codes. Exit codes are a number between 0 and 255, which is returned by any Unix command when it returns control to its parent process. Other numbers can be used, but these are treated modulo 256, so exit -10 is equivalent to exit 246 , and exit 257 is equivalent to exit 1 .

What does exit status 1 mean?

The "Exit Code 1" is simply a "Generic Exit Code" which means the job failed and this can be for any reason.


1 Answers

I think you're looking for the $? shell variable, which gives the exit code of the previous command. For example:

$ diff foo.txt foo.txt
$ echo $?
0

To use this in your makefile, you would have to escape the $, as in $$?:

all:
    diff foo.txt foo.txt ; if [ $$? -eq 0 ] ; then echo "no differences" ; fi

Do note that each command in your rule body in make is run in a separate subshell. For example, the following will not work:

all:
    diff foo.txt foo.txt
    if [ $$? -eq 0 ] ; then echo "no differences" ; fi

Because the diff and the if commands are executed in different shell processes. If you want to use the output status from the command, you must do so in the context of the same shell, as in my previous example.

like image 109
Eric Melski Avatar answered Oct 21 '22 22:10

Eric Melski