3

I have some common code that I would like to execute in the %pre and %post section of my rpm spec file. When I place a subroutine in the %pre section, I need to add the same subroutine to the %post section of the RPM. It sucks maintaining the same subroutine twice. Here is an example:

RPM spec file %pre and %post sections:

%pre
log_file=/var/log/myrpminstall.log
#-------------------------------------
# Send text log_file
#-------------------------------------
log_it() {
  log_msg=$1
  echo -e $log_msg >> $log_file
}

log_it "pre section log information"

%post

log_it "Post section log informations"

Currently, when the %post section of the rpm executes during an install, I receive an error message:

/var/tmp/rpm-tmp.36557: line 5: log_it: command not found

So, is there a way to make a subroutine like log_it accessible to all sections (global function) of the RPM?? Currently, I have to place the log_it function in the %post section if I want to use it there.

5

The reason this doesn't work is because each scriptlet (%post, %pre, etc.) is written as an independent script and passed to bash/sh for execution. Thus the shell that executes it is unaware of any function defined in another scriptlet.

I'd recommend using RPM macros for this purpose. You can put them in ~/.rpmmacros or /etc/rpm/macros. Something like this:

%define log \
log_it() { \
  log_msg=$1 \
  echo -e $log_msg >> $log_file \
}

%pre
%log

log_it test

%post
%log

log_it test

See http://rpm5.org/docs/rpm-guide.pdf for more info or even /usr/lib/rpm/macros.

  • That's what I was looking for..Works perfect now in %pre and %post... – GoinOff Aug 8 '14 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.