help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Executing a shell script in a Makefile


From: Paul D. Smith
Subject: Re: Executing a shell script in a Makefile
Date: Wed, 29 Sep 2004 17:39:33 -0400

%% G2345C <address@hidden> writes:

  g> all :
  g>    if [ -d $(GPS_ENGINE_OBJS_DIR) ] ; then \
  g>       for f in `cd $(GPS_ENGINE_OBJS_DIR) && ls` ; do
  g> \
  g>          case $$f in \
  g>            *.lo)     HB="$(HB) `echo $$f`" ;; \
  g>            *)        ;; \
  g>          esac ; \
  g>       done \
  g>    fi 
  g>    echo $(HB)

  g> I want to put all of the file that name is *.lo into a
  g> string $(HB) how do I do that

  g> Right now it echo empty string $(HB)

You have to be very careful to distinguish between SHELL COMMANDS and
MAKE COMMANDS.  In general any line that starts with a TAB is passed to
a shell to be executed there, it's NOT executed by the make program.
Obviously, a shell script cannot modify the contents of a make variable
directly!  That just can't work.  So this:

   *.lo)     HB="$(HB) `echo $$f`" ;; \

is not doing what you think it is.  First the make variable reference
$(HB) is expanded and resolves to the empty string.  Then the entire
thing is handed to the shell, so when the shell sees it the line looks
like this:

   *.lo)     HB=" `echo $$f`" ;; \

(you can see this for yourself if you look at the output make prints).

This is of course a very complicated way to just say:

   *.lo)     HB=" $$f" ;; \

(the echo just prints the value after all).

And this, remember, is setting the SHELL variable HB, not the MAKE
variable HB.


The next thing to remember is that every logical line in a command
script is invoked in a separate shell.  So, after the if statement where
you don't have a backslash:

      done \
   fi 
   echo $(HB)

that first shell is done and exits, then a new shell is started to run
the echo command.

Well, when the first shell exits the SHELL variable HB is gone; it
existed only in the memory of that shell.  Now the next line you invoke
a new shell and pass the expansion of the MAKE variable HB, which is
still set to nothing, and it echos the empty string.

-- 
-------------------------------------------------------------------------------
 Paul D. Smith <address@hidden>          Find some GNU make tips at:
 http://www.gnu.org                      http://make.paulandlesley.org
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist




reply via email to

[Prev in Thread] Current Thread [Next in Thread]