LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Notify Shell Script from Makefile when got error ??? (https://www.linuxquestions.org/questions/linux-newbie-8/notify-shell-script-from-makefile-when-got-error-753218/)

RockyMarrone 09-07-2009 01:09 AM

Notify Shell Script from Makefile when got error ???
 
Hi folks,
I am writing a shell script which will call another shell scripts.
And those scripts are actually calling Makefiles.
I just want a notification from the makefiles that build is successful or have some error.
I don't want to change the makefiles and dont want to add any export variable in the makefile's as i dont want to change the makefiles.

Structure is like

Shell Script (A)
||
||
Shell Script (B)
||
Makefile 1 / Makefile 2 / Makefile 3 /Makefile 4

Example if any error or success comes in Makefile 1/2/3/4 then Shell Script A should get notification.
We can only modify the Shell Script A. ( A big constraint :) )



Thnx in advance for the help.

thnx & best regards
Rocky

colucix 09-07-2009 02:09 AM

Hi and welcome to LinuxQuestions!

Maybe I don't quite understand the problem, but can't you just let the make command throw its output to the terminal? Or eventually redirect it to some files and then parse them in script A, looking for some keywords that can tell you if it run successfully or not? Even more simple if the make command exits with a proper status code (0 = success, >0 = failure) so that you can simply check it using $? and act accordingly to notify the user.

RockyMarrone 09-07-2009 03:54 AM

Thnx for ur prompt reply first.
I did that i used $? for getting the last error.
But as i m calling a makefile from the shell script so the last error is coming as 0 always i don't know why it is
not capturing the last error of makefile inside the shell script.
And regarding the problem i m explaining it again hope that you can get it.


I am calling a makefile from a shell script to build it. I just need the makefile build status back in the shell script.

i92guboj 09-07-2009 06:43 AM

Quote:

Originally Posted by RockyMarrone (Post 3672766)
Thnx for ur prompt reply first.
I did that i used $? for getting the last error.
But as i m calling a makefile from the shell script so the last error is coming as 0 always i don't know why it is
not capturing the last error of makefile inside the shell script.

How exactly are you using "$?" ?

Quote:

I am calling a makefile from a shell script to build it. I just need the makefile build status back in the shell script.
Code snippets will work better than any amount of words. You usually would use something like this in your shell script to "run" the makefile as you say:

Code:

make -f whatever_makefile
Or simply "make", in any case, it's the return status of the make command what you will be getting. That's zero for normal termination (as usual), 2 for errors, and 1 (and here's the exception) if you are using -q and some file wasn't up to date. That's it if my memory serves correctly.

Usually in the script you would catch $? just after running that command.

Code:

make -f whatever_makefile
if [ $? -eq 0 ]; then
  echo "Everything was ok."
  exit 0
elif [ $? -eq 1 ]; then
  echo "You used make -q, and some files were updated."
  exit 1
elif [ $? -eq 2 ]; then
  echo "There were errors."
  exit 2
else
  echo "Shouldn't ever read this."
  exit 10
fi

A shorthand for this would be to define some die() function like some people do and use it in conjunction with the logical OR ('||' in bash).

Code:

function die() {
  echo "$1"
  exit 1
}

make -f yourmakefile || die "make failed in yourmakefile"

You don't even need to check $? this way.

In your case, I'd start checking that you are not using -q, as you see, when you use it a non-zero value is not necessarily an error, which can cause confusion.

RockyMarrone 09-08-2009 12:05 AM

Hi,
U r absolutlly correct that code can say thousands of words so here is the piece of code.

This is my bash script

#!/bin/bash
# Shell Script for automating the build ....

SAMBA_PATH=$STACK_PATH/Build


function compile() {
echo "Starting Building ........................"
cd $STACK_PATH/Build/
make all
echo "Build Sucessfull !!!!!"

VAR=$?
echo $VAR -------> Value of VAR is always "0" means no error has been detected
------------------> If make file gives error also it always give "0"
------------------> If make file produces some error then i should get notified
}

In the directory lets say i am having a makefile which is just doing

# Sample Makefile

CC = g++
TARGET = test

CXXFLAGS = -g -c -Wall -Werror -O2
DXXFLAGS = -D_LINUX

INCLUDE = -I../inc/
SRC = $(shell find . -name '*.cc')
OBJ = $(SRC:.cc=.o)

$(TARGET) : $(OBJ)
$(CC) -o ../exe/$(TARGET) $(OBJ)

.cc.o :
$(CC) $(CXXFLAGS) $(DXXFLAGS) $(INCLUDE) $< -o $@

So if lets say that there is an error i got while compiling the source code with this file
then i should get a notification in the Bash Script file which is above

So when ever there is an error come in the compilation there should be a notification to the BASH Script file.

colucix 09-08-2009 02:46 AM

Code:

make all
echo "Build Sucessfull !!!!!"

VAR=$?

The built-in variable $? stores the exit status of the last executed command. If the code you're testing is exactly that one posted above, the $? stores the exit status of the echo command, which is always 0. Try to re-organize your code and you will get the expected result.

i92guboj 09-08-2009 06:21 AM

colucix told it so: when we say "the last command" we really mean so. In this case, it would be the exit status of echo, which will be "success" (0).

A quick look at echo.c from current coreutils seems to suggest that it's really impossible to get any other exit status than EXIT_SUCCESS, which I assume is zero.

RockyMarrone 09-16-2009 12:38 AM

Thnx i92guboj...
I am able to solve the problem.
I put the $? at correct location and after that it is working


All times are GMT -5. The time now is 04:27 AM.