Default behaviour for rpm {-q|--query}
without specific select options is to query the RPM database.
Newer versions of the rpm command appear to have improved and recognise that when rpm -q something.that.ends.in.dot.rpm
is used that you intend to query a PACKAGE_FILE rather than query the rpm database.
Older versions don't have that implicit behaviour. Older versions will look for a PACKAGE named something.that.ends.in.dot.rpm
in the RPM database (and typically won't find anything).
That is one problem/difference that you're seeing.
You solve that by explicitly instructing rpm to query a package file with the -p, --package
select option/command switch, which should result in consistent behaviour with all current and past rpm versions. So use :
rpm -q -p PACKAGE_FILE
Check the manual (with man rpm
) to see what capabilities and option the rpm
command provides on a system.
Note: The version information that your system will use when installing a .rpm file comes from the package rpm metadata, not the package filename.
That means you should not evaluate the PACKAGE_FILE filename to determine what PACKAGE and name-version.arch it provides. Although it is convention to use a naming scheme like NAME-version_number.release.arch.rpm
as the PACKAGE_FILE filename for a PACKAGE nothing prevents a situation where foo-1.02.1x86_64.rpm
will contain the meta data and install PACKAGE bar-3.21.1.noarch
(although usually only more subtle differences happen).
Now I think that you want to query the PACKAGE_FILE, see what PACKAGE (package name, version and architecture) it provides and then check the rpm database to see if that version is installed.
Or, the reverse, check the rpm database to see which version(s) of PACKAGE are installed and then see if that is the newer/same/older version compared to the version in PACKAGE_FILE.
Query the PACKAGE_FILE for the PACKAGE it provides according the rpm meta-data stored in that file:
rpm -q -p PACKAGE_FILE
rpm -q -p Package_linux_v12_2_5_1.rpm
That should usually return the PACKAGE as output, typically a name-version.arch string along the lines of:
PACKAGE
respectively:
Package_linux-v12_2_5_1.x86_64
Then query the RPM database to see if that exact PACKAGE is installed:
rpm -q PACKAGE
rpm -q Package_linux-v12_2_5_1.x86_64
That can have two results:
package PACKAGE is not installed
and exitcode 1
- when that exact PACKAGE (name-version.arch) is not installed
PACKAGE
and exitcode 0
- the PACKAGE is installed and the (name-version.arch) of the installed version is shown
Now for automation you might want to use --queryformat QUERYFMT
options to adjust the query output and use something else than name-version.arch
.
For example I'd restrict the query output to only the PACKAGE_NAME. Using the PACKAGE_NAME to search the rpm database will allow me to see if a different version of the PACKAGE_FILE is installed, rather than only searching for the exact same match.
rpm -q --queryformat %{NAME} -p PACKAGE_FILE
PACKAGE_NAME=`rpm -q --queryformat %{NAME} -p Package_linux_v12_2_5_1.rpm`
and then when one (or more) PACKAGES with that NAME are installed searching the RPM database could yield something like:
rpm -q $PACKAGE_NAME
Package_linux-v10_1_3_1.x86_64
Package_linux-v11_2_0_9.x86_64
Final goal: To be able to query both RHEL versions using same method, comparing the existing version and the one to be installed and output the version of the package.
In an un-tested pseudo shell script:
#!/bin/bash
PACKAGE_FILE="/path/to/package-version.arch.rpm"
NAME=`rpm -q --queryformat %{NAME} -p $PACKAGE_FILE`
VERSION=`rpm -q --queryformat %{VERSION} -p $PACKAGE_FILE`
PACKAGE=`rpm -q -p $PACKAGE_FILE`
echo "$PACKAGE_FILE provides - $NAME - $VERSION"
# Check if the exact PACKAGE is installed
OUTPUT=`rpm -q $PACKAGE`
if [ $? -eq 0 ]; then
echo "That exact version is already installed."
exit
fi
# Check if a different version of PACKAGE is installed
OUTPUT=`rpm -q $PACKAGE_NAME`
if [ $? -eq 0 ]; then
echo "A different version of $PACKAGE_NAME is installed."
echo "Currently installed is: $OUTPUT"
exit
else
echo "$PACKAGE is not installed."
fi