Problems with tests in make

Chris Lewis clewis at ferret.ocunix.on.ca
Sat Jun 8 16:11:54 AEST 1991


In article <4378 at polari.UUCP> 6sigma2 at polari.UUCP (Brian Matthews) writes:

Hi Brian, long time no see.

>I'm trying to write some make rules that use test to conditionally
>execute different shell commands.

>The obvious solution is to do something like this:

>	if $(VAR) = val1; then command1; else command2; fi

>If VAR happens to contain val1, or I'm using a System V-like make,
>everything works fine.  However, if VAR doesn't contain val1, BSD-like
>makes seem to bail out as soon as the test fails - the else command2
>isn't executed, and the make fails.

This is a bug in the BSD shell.  To-wit, make starts the shell script
with "-e" set, and the test's failure causes the shell script
to terminate immediately rather than processing thru the else clause.
You can test this yourself with the following shell script:

	set -e
	if test x = y
	then
	    echo wha?
	else
	    echo "worked properly"
	fi

On non-SYSV shells this won't print anything.  I think this botch was
even in V7.

What I usually do is this:

	#	comment out on System V.
	IGNORESH = set +e ;

	all:
		$(IGNORESH) if .... \
		    then
		    ...

Unfortunately, you have to check the returns of each subcommand
individually (as you would do in a normal script), and on error execute
"exit 1".

Psroff has this in its makefiles, and even a way of testing whether you
need it commented out or not.

BTW: go multi line on your shell scripts, it makes them a lot easier to
read even if you have to put backslashes on the ends of all of the lines...

If it wasn't for this stupid botch in BSD, set -e and trap 0's would make error
recovery in shell scripts easy and portable...  Grrr!
-- 
Chris Lewis, Phone: (613) 832-0541, Domain: clewis at ferret.ocunix.on.ca
UUCP: ...!cunews!latour!ecicrl!clewis; Ferret Mailing List:
ferret-request at eci386; Psroff (not Adobe Transcript) enquiries:
psroff-request at eci386 or Canada 416-832-0541.  Psroff 3.0 in c.s.u soon!



More information about the Comp.unix.programmer mailing list