[net.unix] Using sh in makefiles

silvert@dalcs.UUCP (Bill Silvert) (06/27/86)

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?

chris@umcp-cs.UUCP (Chris Torek) (06/28/86)

In article <1991@dalcs.UUCP> silvert@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@umcp-cs		ARPA:	chris@mimsy.umd.edu

gwyn@brl-smoke.ARPA (Doug Gwyn ) (06/29/86)

In article <1991@dalcs.UUCP> silvert@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?

I really don't understand why you've had any problems:

test:
	if x; then y; fi

OR

test:
	if x ; \
	then	y ; \
	fi

Perhaps you're using one of those antique versions of UNIX that has a
"make" and shell that conspire to report that the "if" command has
failed; if so, just use a dash prefix on the command:

test:
	-if x; \
	then	y; \
	fi

I would like to once again plea that people posting problems
tell us what version of UNIX they're running, since there are
several and they don't all behave the same way.

guy@sun.uucp (Guy Harris) (06/29/86)

> 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?

Nope.  If "make" recognizes any of a number of special characters in a
command line, it passes that command line to "sh" rather than running the
command itself.  ";" is one of those characters, so

	test:
		if x; then y; fi

is sufficient.

Furthermore, they must all be on one "line"; however, "make"s idea of a
"line" can be altered by using escaped newlines:

	test:
		if x; then \
			y; \
		fi

will work.
-- 
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com (or guy@sun.arpa)