开发者

running grep from within GNU make

开发者 https://www.devze.com 2022-12-09 23:06 出处:网络
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:

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)' Text开发者_高级运维File)

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.


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


Do you really need to use $(shell) ?

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

all:
  echo ${GrepResult}

Tested with GNU Make 3.81.

0

精彩评论

暂无评论...
验证码 换一张
取 消