Product SiteDocumentation Site

4.2.7. Listing the scripts in a package

RPM packages can have preinstallation, postinstallation, preuninstallation, and postuninstallation scripts. These are scripts that the rpm command will execute before and after installing a package, as well as before and after removing the package. The --scripts option to the rpm –q command lists the scripts associated with a package. The basic syntax follows:
rpm -q --scripts package_name
For example:
# rpm -q --scripts tcsh
postinstall scriptlet (through /bin/sh):
if [ ! -f /etc/shells ]; then
echo "/bin/tcsh" >> /etc/shells
echo "/bin/csh" >> /etc/shells
else
grep '^/bin/tcsh$' /etc/shells > /dev/null || echo "/bin/tcsh" >> /etc/shell
s
grep '^/bin/csh$' /etc/shells > /dev/null || echo "/bin/csh" >> /etc/shells
fi
postuninstall scriptlet (through /bin/sh):
if [ ! -x /bin/tcsh ]; then
grep -v '^/bin/tcsh$' /etc/shells | grep -v '^/bin/csh$'> /etc/shells.rpm
mv /etc/shells.rpm /etc/shells
fi
PRODUCTION: NOTE THE “fi” on a line by itself at the end of this listing. Thanks, -Eric
The simple scripts shown here add an entry to the /etc/shells file and clean up the entry when uninstalled.
Cross Reference
Cleaning up any changes your package makes on uninstallation is a very good idea. See the chapters in Part II, Creating RPMs, for details on making your own well-behaved packages.
Other packages have more complex scripts, as shown following:
# rpm -q --scripts sendmail
preinstall scriptlet (through /bin/sh):
/usr/sbin/useradd -u 47 -d /var/spool/mqueue -r -s /dev/null mailnull >/dev/null
2>&1 || :
postinstall scriptlet (through /bin/sh):
#
# Convert old format to new
#
if [ -f /etc/mail/deny ] ; then
cat /etc/mail/deny | \
awk 'BEGIN{ print "# Entries from obsoleted /etc/mail/deny"} \
{print $1" REJECT"}' >> /etc/mail/access
cp /etc/mail/deny /etc/mail/deny.rpmorig
fi
for oldfile in relay_allow ip_allow name_allow ; do
if [ -f /etc/mail/$oldfile ] ; then
cat /etc/mail/$oldfile | \
awk "BEGIN { print \"# Entries from obsoleted /etc/mail/$oldfile
\" ;} \
{ print \$1\" RELAY\" }" >> /etc/mail/access
cp /etc/mail/$oldfile /etc/mail/$oldfile.rpmorig
fi
done
#
# Oops, these files moved
#
if [ -f /etc/sendmail.cw ] ; then
cat /etc/sendmail.cw | \
awk 'BEGIN { print "# Entries from obsoleted /etc/sendmail.cw" ;} \
{ print $1 }' >> /etc/mail/local-host-names
cp /etc/sendmail.cw /etc/sendmail.cw.rpmorig
fi
#
# Rebuild maps (next reboot will rebuild also)
#
{ /usr/bin/newaliases
for map in virtusertable access domaintable mailertable
do
if [ -f /etc/mail/${map} ] ; then
/usr/bin/makemap hash /etc/mail/${map} < /etc/mail/${map}
sleep 1
fi
done
} > /dev/null 2>&1
/sbin/chkconfig --add sendmail
preuninstall scriptlet (through /bin/sh):
if [ $1 = 0 ]; then
/etc/rc.d/init.d/sendmail stop >/dev/null 2>&1
/sbin/chkconfig --del sendmail
fi
postuninstall scriptlet (through /bin/sh):
if [ "$1" -ge "1" ]; then
/etc/rc.d/init.d/sendmail condrestart >/dev/null 2>&1
fi
exit 0
In this case, the main script is the post-install script, which tries to convert old-format data into the new format, thereby helping users upgrade to the newer release.