LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-07-2009, 02:09 AM   #1
RockyMarrone
LQ Newbie
 
Registered: Sep 2009
Posts: 8

Rep: Reputation: 0
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

Last edited by RockyMarrone; 09-07-2009 at 02:23 AM.
 
Old 09-07-2009, 03:09 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
 
Old 09-07-2009, 04:54 AM   #3
RockyMarrone
LQ Newbie
 
Registered: Sep 2009
Posts: 8

Original Poster
Rep: Reputation: 0
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.
 
Old 09-07-2009, 07:43 AM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 387Reputation: 387Reputation: 387Reputation: 387
Quote:
Originally Posted by RockyMarrone View Post
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.
 
Old 09-08-2009, 01:05 AM   #5
RockyMarrone
LQ Newbie
 
Registered: Sep 2009
Posts: 8

Original Poster
Rep: Reputation: 0
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.

Last edited by RockyMarrone; 09-08-2009 at 01:24 AM.
 
Old 09-08-2009, 03:46 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
 
Old 09-08-2009, 07:21 AM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 387Reputation: 387Reputation: 387Reputation: 387
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.
 
Old 09-16-2009, 01:38 AM   #8
RockyMarrone
LQ Newbie
 
Registered: Sep 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Thnx i92guboj...
I am able to solve the problem.
I put the $? at correct location and after that it is working
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What does $^ means in Makefile or in shell script kondrara Linux - Newbie 1 06-13-2009 03:28 AM
create shell script in makefile xface66 Programming 2 05-08-2007 05:26 AM
Makefile call shell script thllgo Programming 4 12-28-2005 10:51 AM
makefile, shell commands error sateeshgalla Programming 4 11-14-2005 11:57 PM
Shell Script and makefile thomasmathew Programming 11 12-23-2003 04:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration