Jay Taylor's notes

back to listing index

How to get exit status of a shell command used in GNU Makefile?

[web search]
Original source (stackoverflow.com)
Tags: shell-scripting make stackoverflow.com
Clipped on: 2015-10-02

I have a makefile rule in while I am executing a linux tool. I need to check the exit status of the tool command, and if that command fails the make has to be aborted.

I tried checking with $?, $$? \$? etc in the makefile. But they gives me syntax error when makefile runs.

What is the right way to do this ?

Here is the relevant rule in Makefile

    mycommand \
    if [ $$? -ne 0 ] \
    then \
        echo "mycommand failed" \
        false \
    fi
asked May 1 '13 at 8:36
Image (Asset 2/3) alt=
Lunar Mushrooms
1,74131848
   upvote
  flag
Have you tried echo $?? This can help --> cyberciti.biz/faq/… –  fedorqui May 1 '13 at 8:37
   upvote
  flag
Eh? Make automatically checks the exit status of the tool, and aborts on error. A simple mycommand suffices, no? –  bobbogo May 1 '13 at 9:10
3 upvote
  flag
The syntax error comes from make removing the line breaks before passing the text block to the shell. Simply add ;s to the end of each line (apart from the then and fi) and your snippet will lose its syntax errors. –  bobbogo May 1 '13 at 9:11
   upvote
  flag
@bobbogo, thanks it worked. The syntax error was due to missing ; –  Lunar Mushrooms May 1 '13 at 10:27
up vote 7 down vote accepted

In the makefile-:

mycommand || (echo "mycommand failed $$?"; exit 1)

Each line in the makefile action invokes a new shell - the error must be checked in the action line where the command failed.

If mycommand fails the logic branches to the echo statement then exits.

answered May 1 '13 at 8:49
Image (Asset 3/3) alt=
suspectus
6,61651735
1 upvote
  flag
Thanks , I had to use $$? too –  Lunar Mushrooms May 1 '13 at 10:35

Your Answer

asked

2 years ago

viewed

5539 times

active

2 years ago

Technology Life / Arts Culture / Recreation Science Other
  1. Stack Overflow
  2. Server Fault
  3. Super User
  4. Web Applications
  5. Ask Ubuntu
  6. Webmasters
  7. Game Development
  8. TeX - LaTeX
  1. Programmers
  2. Unix & Linux
  3. Ask Different (Apple)
  4. WordPress Development
  5. Geographic Information Systems
  6. Electrical Engineering
  7. Android Enthusiasts
  8. Information Security
  1. Database Administrators
  2. Drupal Answers
  3. SharePoint
  4. User Experience
  5. Mathematica
  6. Salesforce
  7. ExpressionEngine® Answers
  8. more (13)
  1. Photography
  2. Science Fiction & Fantasy
  3. Graphic Design
  4. Movies & TV
  5. Seasoned Advice (cooking)
  6. Home Improvement
  7. Personal Finance & Money
  8. Academia
  9. more (9)
  1. English Language & Usage
  2. Skeptics
  3. Mi Yodeya (Judaism)
  4. Travel
  5. Christianity
  6. Arqade (gaming)
  7. Bicycles
  8. Role-playing Games
  9. more (21)
  1. Mathematics
  2. Cross Validated (stats)
  3. Theoretical Computer Science
  4. Physics
  5. MathOverflow
  6. Chemistry
  7. Biology
  8. more (5)
  1. Stack Apps
  2. Meta Stack Exchange
  3. Area 51
  4. Stack Overflow Careers
site design / logo © 2015 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2015.9.30.2859