在 Makefile 中使用 shell 查找 Ubuntu 版本

Using shell in Makefile to find Ubuntu Version

我正在尝试为我的工具制作 Ubuntu 版本特定的 disros,所以我想获得 os 名称和版本。我有以下代码:

ifeq ($(OS),Windows_NT)
    OS_POD+=win
else
    UNAME_S := $(shell uname -s)
    ifeq ($(UNAME_S),Linux)
            OS_VERS := $(shell lsb_release -a 2>/dev/null | grep Description | awk '{ print  "-"  }')
            OS_POD=./dist/linux/$(OS_VERS)
    endif
    ifeq ($(UNAME_S),Darwin)
            OS_POD=./dist/mac
    endif
endif

我使用 shell 单线:

lsb_release -a 2>/dev/null | grep Description | awk '{ print  "-"  }'

returns Ubuntu-12.04.2 在 Makefile 外部,但在 Makefile 内部 returns 什么都没有。也就是说,OS_VERS 变量只是 -.

我该如何解决这个问题?

在 Makefile 中,$ 是特殊的。在您希望 shell 找到一美元的地方使用 $$

OS_VERS := $(shell lsb_release -a 2>/dev/null | grep Description | awk '{ print $ "-" $ }')

您需要在命令中转义 $

OS_VERS:=$(shell lsb_release -a 2>/dev/null | grep Description | awk '{ print $ "-" $ }')

以下示例 Makefile 打印正确,因此它可能是您的 Makefile 的不同部分。

print: 
    @echo $(OS_VERS)

OS_VERS:=$(shell lsb_release -a 2>/dev/null | grep Description | awk '{ print $ "-" $ }')

OS_VERS := $(shell cat /etc/os-release | grep ^NAME | cut -d'=' -f2 | sed 's/\"//gI')