jackhab
jackhab

Reputation: 17698

running grep from within GNU make

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.

Upvotes: 7

Views: 8481

Answers (2)

Beta
Beta

Reputation: 99104

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)')

Upvotes: 8

ankon
ankon

Reputation: 4215

Do you really need to use $(shell) ?

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

all:
  echo ${GrepResult}

Tested with GNU Make 3.81.

Upvotes: 1

Related Questions