未从 grep 输出设置 Makefile 变量

Makefile variable not set from grep output

我正在尝试将变量 COGLINE 设置为我的 grep 行的输出(它正在我的 config.json 文件中搜索 regExthe "cogs")。当我执行 grep 行时,它会正确输出正确的行号,但是当我回显变量时,它会变成空白。

COGLINE = $(grep -n \"cogs\" ~/Desktop/Repos/pronghorn/config.json | cut -f1 -d:)

all:
    grep -n \"cogs\" ~/Desktop/Repos/pronghorn/config.json | cut -f1 -d:
    echo $(COGLINE)

这是输出:

GlennMBP:test glenn$ make all
grep -n \"cogs\" ~/Desktop/Repos/pronghorn/config.json | cut -f1 -d:
2
echo 

您可以看到行号被正确地找到为“2”,但是该变量出现空白,就好像它没有被设置一样。我做错了什么?

grep 不是 make 函数。 COGLINE = 行是 make 赋值。

您要么需要使用

COGLINE := $(shell grep -n \"cogs\" ~/Desktop/Repos/pronghorn/config.json | cut -f1 -d:)

如果您希望 运行 在 make 解析时将其放在 make 变量中。

all:
        COGLINE=$$(grep -n \"cogs\" ~/Desktop/Repos/pronghorn/config.json | cut -f1 -d:); \
        echo "$${COGLINE}"

all 配方执行时将其 运行 并保存在 shell 变量中。

也有中间立场,但这是两个基本想法。