Using sh in makefiles

Chris Torek chris at umcp-cs.UUCP
Sat Jun 28 21:15:05 AEST 1986


In article <1991 at dalcs.UUCP> silvert at dalcs.UUCP (Bill Silvert) writes:
>Would like advice on how to use shell commands (such as if...) in
>makefiles.  I know that it all has to be on one line, but the only way I
>can make a command work is through a kludge like:
>
>test:
>	echo "if x;then y;fi" | sh
>
>which seems pretty roundabout.  Is this the only way to do it?

No.  Here is an example of a series of shell commands I use in a
particular Makefile:

	# make the distribution directory---assumes no RCS files in top level
	# `doc' is not yet ready
	dist:
		rm -rf ../ctex_dist
		-mkdir ../ctex_dist
		-set -x +e; \
		 for i in *; do \
			if [ $$i != ctex -a $$i != doc -a $$i != misc ]; then \
				if [ -d $$i ]; then \
					cp -r $$i ../ctex_dist; \
					(cd ../ctex_dist/$$i; \
					 co -q RCS/*,v; \
					 rm -rf RCS); \
				else \
					cp $$i ../ctex_dist; \
				fi; \
			fi; \
		 done
		-cd ../ctex_dist; \
		 echo '/MANDIR=	/s,local/,,XwXq' | \
		 tr X '\012' | ed Makefile
		-cd ../ctex_dist; make clean
		-cd ../ctex_dist/dev; make dist

Note in particular the `set +e' in the command containing the `for'.
It runs various commands, in particular /bin/[, that exit with a
nonzero status; this makes some `sh's exit even when the command
is part of an `if'.  The `set +e' avoids the problem in all `sh'
variants.  (The exit occurs because make runs `sh -ce "commands"',
which sets -e: exit on error.  Using `echo "string" | sh' avoids
this problem as well, but at the expense of an extra process.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.unix mailing list