使用 awk 提取 Makefile 中的特定文本

Using awk to extract a specific text in a Makefile

我有一个这种格式的配置文件

foo=bar
fie=boo
..
..

还有一个 Makefile,我想提取一行包含字符串 'disk_size' 的配置文件,然后提取分配给变量的值

这是我在 Makefile 中使用的行

fallocate -l $(shell awk -F= '/disk_size/ { print  }' $(conf)) $@ 

但是我收到这个错误,(整行都被提取了。)

fallocate -l disk_size=268435456 disk.img
fallocate: invalid length value specified

awk 命令在终端中有效,但在 Makefile 中无效,为什么?

tnx

您可能只需要转义 $:

fallocate -l $(shell awk -F= '/disk_size/ { print $ }' $(conf)) $@ 

Make 正在尝试使用变量 </code> 而不是将字符串 <code> 传递给 awk。

如果你的配置文件确实是这种格式:

foo = bar
disk_size = 1234

您可以直接将其包含在 Makefile 中:

# Include configuration file
include $(conf)

target:
    fallocate -l $(disk_size)

你也可以使用-运算符来忽略include命令的错误并在没有配置文件时分配默认值。

# Include configuration file
-include $(conf)

# Set default size
disk_size ?= 5678

target:
    fallocate -l $(disk_size)