LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   scripting: How to check if configure or make fails? (https://www.linuxquestions.org/questions/linux-newbie-8/scripting-how-to-check-if-configure-or-make-fails-676733/)

klss 10-16-2008 04:16 AM

scripting: How to check if configure or make fails?
 
Hi folks.

My first post over here at this place. I am a kind of newbie when it comes to advanced scripting and programming.

I've written a script to install Alsa manually:

http://ubuntuforums.org/showthread.p...18#post5792918


The problem is, if a "./configure" or "make" fails during the process, the whole installation gets corrupted.

Within the script I want to check before running the "make install" for each package, if all of the "./configure" and "make" finished without error.

What would be the best way to do this?

THX for your support.

KLSS

rigormortis 10-16-2008 04:34 AM

./configure && make && make install

Agrouf 10-16-2008 04:35 AM

Do you know about the return code of commands?
It tells you if a command failed or succeeded.
it's called $? from the shell
For instance, let's do a simple command and see the return code:
$ ls
$ echo $?
It will print the return code of ls, which is 0 if succeeded and something else if failed.
you can use this code to take action:
$ ls
$ if [ "$?" -ne 0 ]
> then
> echo "Failed"
> fi

You can also use the || and && operators like this:
$ ls || echo "Failed"
$ make || exit 1
$ make && echo "success"

i92guboj 10-16-2008 06:59 AM

Quote:

Originally Posted by Agrouf (Post 3311925)
Do you know about the return code of commands?
It tells you if a command failed or succeeded.
it's called $? from the shell
For instance, let's do a simple command and see the return code:
$ ls
$ echo $?
It will print the return code of ls, which is 0 if succeeded and something else if failed.
you can use this code to take action:
$ ls
$ if [ "$?" -ne 0 ]
> then
> echo "Failed"
> fi

You can also use the || and && operators like this:
$ ls || echo "Failed"
$ make || exit 1
$ make && echo "success"

Yep. It all sums up to return codes.

In a script people usually do something in the lines of this:

Code:

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

./configure || die "configure failed"
make || die "make failed"
su -c 'make install' || die "su or make install failed"

This can be extended to any imaginable limit. The gentoo ebuilds system uses this intensively.

The most basic version would work the same, just chaining them with &&

Code:

./configure && make && su -c 'make install'
If one fails, the next one will not be run. You could use an additional OR to exit or something

Code:

(./configure && make && su -c 'make install') || echo myerror && exit 1

klss 10-16-2008 07:36 AM

Thx a lot folks.

I'll implement your suggestions.

Just a question:

Would it be possible to put the whole process also in a
"while" loop? while true do? Just an idea.

THX again.

Klss

Agrouf 10-16-2008 08:37 AM

indeed, do it like this:
while true
do
echo "I must do something here"
[ $? -eq 0 ] || break
done

this will print 'I must do something here' indefinitely, until it fails, which should never happen anyway but...
I don't get how you want to use it for configure and make and make install...

klss 10-16-2008 10:50 AM

Quote:

Originally Posted by Agrouf (Post 3312187)
indeed, do it like this:
while true
do
echo "I must do something here"
[ $? -eq 0 ] || break
done

this will print 'I must do something here' indefinitely, until it fails, which should never happen anyway but...
I don't get how you want to use it for configure and make and make install...

Never mind. ;)

I just introduced above proposals to the Alsa upgrade script. I'll see if the script safely runs for the majority of people.

Thx again.


All times are GMT -5. The time now is 09:29 AM.