2015-04-22 116 views
0

我想設置變量COGLINE是我的grep行(這是搜索我的config.json文件的regExthe「齒輪」)的輸出。當我執行grep行時,它會正確輸出正確的行號,但當我回顯變量時,它會變爲空白。Makefile變量未設置從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」,但如果它未設置變量出現空白。我究竟做錯了什麼?

回答

1

grep不是make功能。那COGLINE =行是一個make作業。

你要麼需要使用

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

,如果你想在化妝解析時間運行,並希望它在make變量。

或者

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

all配方執行時間運行它,它有一個shell變量。

也有中間的理由,但這些是兩個基本的想法。