Running grep from within GNU make

I need to find the text 'ifeq ($ (Param1)' using grep. I am trying to assign a search result to create a variable. The problem is that single quotes do not skip text in make so when I try:

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

I get:

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

$ can be done with $$, but how do I avoid parentheses in make? Thanks.

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

+6
regex grep makefile
source share
2 answers

Do you really need to use $ (shell)?

 GrepResult:= `grep 'ifeq (\$$(Param1)' TextFile` all: echo ${GrepResult} 

Tested with GNU Make 3.81.

+1
source share

The trick is to drag special characters past Make and grep.

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

Make $$ queues in $, then grep turns \ $ into $. Also note that this assignment uses curly braces "{}" rather than parentheses "()" so as not to confuse match results. (There may be a more reliable way to handle the string, but it doesn't matter.)

When you use the result, use single quotes:

  all:
     @echo '$ (GrepResult)'

This has also been tested with GNUMake 3.81.

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

  $ (error '$ (GrepResult)')
+6
source share

All Articles