A bash script to retry large compilations via 'make'

Jun 25, '04 10:15:00AM

Contributed by: MCote

When compiling large programs/libraries, a segmentation fault can happen from the compiler. This can be frustrating when it takes several hours to compile everything since you have to start 'make' manually until it works. I found out an elegant way to automatically start the compilation until the job is completed. In bash, the return value of the last executed program is stored in the $? variable. For example:

$ mkdir existingdir 
$ cd existingdir 
$ echo $? 
0 
$ cd nonexistingdir 
-bash: cd: nonexistingdir: No such file or directory 
$ echo $? 
1
Note that a successfully run program usually returns 0. That being said, it is easy to check whether your building program (e.g. make) succeeded, and if not, to start it back up. I used the following bash loop:
$ while [ $? != 0 ] ; do make ; done
Of course, if the program/library doesn't compile (for other reasons than a segmentation fault by the compiler), this will create an infinite loop that you might want to avoid. I therefore wrote a small script that tries to build five times before stopping:
#!/bin/bash 
NBTRIES=0 
# run invalid command first 
cd /dev/null 
while [[ $? != 0 && $NBTRIES < 5 ]] 
do 
  let NBTRIES=NBTRIES+1 
  make 
done 
After running this script, I can go to sleep without any worries ;)

Comments (8)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040624105638513