31

I have a spec file. I need to %define a spec variable that gets its value from a one line file on the system.

For example

%define path `cat /home/user/path_file`

and in path_file is one line

/var/www/html/hosts

This partially works. I say that begins in the RPM BUILD output sometimes the value of ${path} is literally my command cat /home/user/path_file and sometimes the value is the line in the path_file (/var/www/html/hosts) as it should be?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
last_shogun
  • 337
  • 1
  • 4
  • 8

1 Answers1

42

You can define rpmbuild variables with %(cmd) at the top of the spec file. Notice the command is in parenthesis, not curly brackets. An example:

%define whoami %(whoami)

And elsewhere in the spec file, such as a script or the build/install sections, use the variable as normal in the curly brackets like this:

echo "The user that built this is %{whoami}"

The cmd can be anything, including your cat command. Be careful - when another user rebuilds the spec file it may not find the file. So it'll be preferable to use the %{sourcedir} macro like this:

%define path %(cat %{sourcedir}/path_file)

And make sure that path_file is in the source directory and included as a source in the spec file.

Corey Henderson
  • 7,239
  • 1
  • 39
  • 43
  • Corey - if I want /home/user/path_file to be "Required" for the rpm to run? Can I make it a PreReq or Required parameter in my spec file? When I try required it says the file is not there and it totally is there? – last_shogun Apr 23 '12 at 15:59
  • The PreReq, etc options for depending on files being installed on the system actually look in the rpm database for the existence of a package that own the file. It doesn't check the filesystem for the existence of the file. – Corey Henderson Apr 23 '12 at 17:31
  • 1
    @last_shogun There are ways outside of prereq, etc though, such as having a %pre script that checks for the existence of the file, and if not there, echos an error message and exits with a non-zero status. – Corey Henderson Apr 23 '12 at 17:34