Product SiteDocumentation Site

Specifying parameters to macros

Most macros perform simple text substitution. You can also pass parameters to macros, and access those parameters within your macros, similarly to how shell scripts get command-line parameters.
Cross Reference
Chapter 14, Automating RPM with Scripts covers shell scripting with RPM.
With parameters, you can expand the normal definition of a macro to the following:
%define macro_name(options) value
Any text within the parenthesis is passed to getopt(3), and acts as parameters to the macro. This is performed when the macro is expanded. You can also pass options to the macro using the %macro_name syntax (without curly braces). For example:
%foo 1 2 3
This example passes the parameters 1, 2, and 3 to the macro foo. Inside the macro, you can use a shell script-like syntax to access the parameters through special macros. Table 10-6 lists these macros.
Table 10-6 Parameter macros inside a macro expansion
Macro
Holds
%0
The name of the macro
%*
All the parameters to the macro, except for any processed options
%#
The number of parameters
%1
The first parameter
%2
The second parameter
%3
The third parameter, and so on with %4, %5 and beyond
%{-p}
Holds -p if the -p parameter was passed to the macro; otherwise holds nothing
%{-p*}
Holds the value passed with the -p parameter, if the -p parameter was passed to the macro; otherwise holds nothing
%{-p:text}
Holds text if the -p parameter was passed to the macro; otherwise holds nothing
Note that all parameters listed in Table 10-6 hold the remaining parameters after getopt(3) processing. You can use these macros within the definition of your own macros. You can also nest macros, such as the following:
%define mypatch() patch %{-p:-p%{-p*}}
This macro expands to the patch command if no -p parameter was passed. If you pass a -p parameter, such as -p 1, then the macro expands to -p with the value of the -p parameter:
patch -p1
Note
This type of syntax is used heavily with the patch command.