views:

278

answers:

2

I need to find the text 'ifeq ($(Param1)' using grep. I try to assign search result to make variable. The problem is that single quotes don't escape text in make so when I try:

GrepResult:= $(shell grep 'ifeq ($$(Param1)' TextFile)

I get:

Makefile:214: *** unterminated call to function `shell': missing `)'.  Stop.

The $ can be escaped with $$ but how do I escape parentheses in make? Thanks.

NB: $GrepResult is used in $(error) function, not in a rule command.

A: 

Do you really need to use $(shell) ?

GrepResult:= `grep 'ifeq (\$$(Param1)' TextFile`

all:
  echo ${GrepResult}

Tested with GNU Make 3.81.

ankon
sorry, I didn't mention it, but I use grep result with $(error) and with back-quotes it simply outputs the variable contents without executing command.
Jack
+2  A: 

The trick is to smuggle the special characters past Make and grep.

GrepResult := ${shell grep 'ifeq (\$$(Param1)' TextFile}

Make turns $$ into $, then grep turns \$ into $. Also note that this assignment uses curly braces "{}", not parentheses "()", so as not to be confused by the results of the match. (There may be a more robust way to handle the string, but never mind.)

When you use the result, use single quotes:

all:
    @echo '$(GrepResult)'

This too was tested with GNUMake 3.81.

EDIT: This also works with $(error...):

    $(error '$(GrepResult)')
Beta