如何在 shell 脚本中从另一个 Makefile 获取变量

Jia*_*Liu 2 arch-linux make kernel-modules shell-script

~/kernelbuild/linux-3.14.37我想在我自己的 shell 脚本中使用该目录下另一个 Makefile 中的变量。

以下是我需要的变量:

VERSION = 3
PATCHLEVEL = 14
SUBLEVEL = 37
Run Code Online (Sandbox Code Playgroud)

问题是我不允许更改 Makefile,因此导出变量不是这里的选项。

另外,我看到有人说要有一个单独的文件,其中包含所需的所有常见变量。这很实用,但我需要将所有内容都放在我的单个 shell 脚本中

我试图包含. ~/kernelbuild/linux-3.14.37/Makefile在我的 shell 脚本中,我认为我应该能够使用这些变量,但它给了我以下错误:

/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 1: VERSION: command not found
/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 2: PATCHLEVEL: command not found
/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 3: SUBLEVEL: command not found
/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 4: EXTRAVERSION: command not found
/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 5: NAME: command not found
/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 17: MAKEFLAGS: command not found
/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 20: unexport: command not found
/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 26: unexport: command not found
/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 47: syntax error near unexpected token `"$(origin V)",'
/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 47: `ifeq ("$(origin V)", "command line")'
Run Code Online (Sandbox Code Playgroud)

我认为这是因为 Makefile 不遵循 shell 脚本的语法规则。那我该怎么办呢?

提前致谢!

der*_*ert 5

如果您只需要简单的东西(例如您的示例),只需将grep它们从 Makefile 中取出即可。

对于更复杂的事情,GNU Make 有一个-p选项,可以在运行 Make 后打印数据库,其中包括所有变量定义(以及更多)。您可以将它与 一起使用-n,这会导致操作实际上并未运行(因此不会构建任何内容)。您可以将grep变量从-p输出中取出。