I have the following scenario:I install version 1 of a package, it does custom operations during post install (copying some files to a location)I install version 2 of the same packageI want to uninstall version 1. To be able to do the cleanup I'll need to know the exact version during the post uninstall, to clean up the files that were copied.Is there a way to get the version number in a rpm scriptlet?
How to get version to be uninstalled inside rpm scriptlet?
rpm
The % scripts are called from the relevant version of the rpm file.  So if you upgrade from version 1.0 to 1.1 then the pre/post install scripts are run from the 1.1 version of the rpm and the pre/post uninstall scripts are run from the 1.0 version.This makes it quite easy for the scripts to handle their own version properly.For example, here's an extract from a SPEC file%preecho Pre called with version %{version}-%{release}%postecho Post called with version %{version}-%{release}%preunecho Pre-un called with version %{version}-%{release}%postunecho Post-un called with version %{version}-%{release}They are very simple, just to demonstrate this.  When you build the rpm then these values are embedded into the rpm eg% rpm -q --scripts -p sweh-test-1.0-0.x86_64.rpm preinstall scriptlet (using /bin/sh):echo Pre called with version 1.0-0postinstall scriptlet (using /bin/sh):echo Post called with version 1.0-0preuninstall scriptlet (using /bin/sh):echo Pre-un called with version 1.0-0postuninstall scriptlet (using /bin/sh):echo Post-un called with version 1.0-0Now let's install version 1.0.0 of this package:% sudo rpm -i sweh-test-1.0-0.x86_64.rpm Pre called with version 1.0-0Post called with version 1.0-0So far, pretty expected.  The pre/post install scripts are run.Now let's upgrade to version 1.1:% sudo rpm -U sweh-test-1.1-0.x86_64.rpmPre called with version 1.1-0Post called with version 1.1-0Pre-un called with version 1.0-0Post-un called with version 1.0-0Note the order of the calls and the version numbers; pre/post install are called and the version-release matches the new version, and then pre/post uninstall are called with the old version number.So the idea is that you don't need to derive this information, you can include it directly in the rpm spec file via use of the % macros.