Issue
I have a very simple RPM spec file that installs one binary and a service file depending on the init system. I have a macro that checks the init system that is defined like so:
%define init_system $(/bin/awk -F '[()]' '{printf $2}' /proc/1/stat)
The command works ok and in fact, a simple statement like
%pre
echo "%{init_system}"
outputs systemd
as expected when the rpm is installing.
The problem I have encountered is that it doesn't seem to work when used in a conditional statement e.g.
%pre
%if "%{init_system}" == "systemd"
echo "using systemd"
%else
echo "using another init system: %{init_system}"
%endif
The output of this is: using another init system: systemd
I initially got around the problem by changeing to simple bash scripting:
if [[ "%init_system" == "systemd" ]]
then
echo "using systemd"
else
echo "using another init system: %{init_system}"
fi
and this correctly prints using systemd
.
The bash way does not work at the %file
section though as it expects %if
or files starting with /
Any suggestions as to why the original method didn't work?
Solution
The problem I have encountered is that it doesn't seem to work when used in a conditional statement
Well no, it wouldn't. Conditional directives in spec files are evaluated by rpmbuild
, not by a shell. rpmbuild
will expand the macro, but the result in this case is just text to it.
The output of this is:
using another init system: systemd
That's natural, because the the text other than the conditional directives is handled as input to a shell. That shell performs command expansion on the result of RPM's macro expansion.
I initially got around the problem by changeing to simple bash scripting:
Yes, that's appropriate.
The bash way does not work at the %file section though as it expects %if or files starting with /
The particular conditional in the example is not sensible for the %files
section in anyway, because that section has its full effect during RPM building, not installation. You cannot use such an approach to modulate which files are installed based on installation-host characteristics.
In the event that you want rpmbuild
to execute a command on the build host and capture its output, use %(...)
. It works much like $(...)
does in the shell. This generally makes sense only for probing characteristics for which you expect every supported installation target to exactly match the build host, or where it is only the details of the build host that matter.
Answered By - John Bollinger Answer Checked By - Mary Flores (WPSolving Volunteer)