RPM spec文件 - 是否可以动态填充spec文件变量

las*_*gun 30 rpm rpmbuild rpm-spec

我有一个spec文件.我需要定义一个spec变量,它从系统上的一行文件中获取其值.

例如

%define path `cat /home/user/path_file`
Run Code Online (Sandbox Code Playgroud)

并且在path_file中是一行

/var/www/html/hosts
Run Code Online (Sandbox Code Playgroud)

这部分有效.我说从RPM BUILD输出开始有时值${path}实际上是我的命令cat /home/user/path_file,有时值是path_file(/ var/www/html/hosts)中的行应该是什么?

Cor*_*son 42

您可以%(cmd)在spec文件的顶部定义rpmbuild变量.请注意,命令在括号中,而不是大括号.一个例子:

%define whoami %(whoami)
Run Code Online (Sandbox Code Playgroud)

在spec文件的其他地方,例如脚本或构建/安装部分,在大括号中正常使用变量,如下所示:

echo "The user that built this is %{whoami}"
Run Code Online (Sandbox Code Playgroud)

cmd可以是任何东西,包括你的cat命令.注意 - 当另一个用户重建spec文件时,它可能找不到该文件.所以最好像这样使用%{sourcedir}宏:

%define path %(cat %{sourcedir}/path_file)
Run Code Online (Sandbox Code Playgroud)

并确保它path_file位于源目录中并作为源文件包含在spec文件中.