I'm maintaining a list of rpm and it's version that needs to be installed
sample packages list below
# Package Version Release Filename
#----------------------------------------------------------------------------------------------------------------------------
mongo-10gen 2.2.0 mongodb_1.x86_64 mongo-10gen-2.2.0-mongodb_1.x86_64.rpm
mongo-10gen-server 2.2.0 mongodb_1.x86_64 mongo-10gen-server-2.2.0-mongodb_1.x86_64.rpm
cpio 2.10 11.el6_3.x86_64 cpio-2.10-11.el6_3.x86_64.rpm
And I'm checking whether the package is already installed and if it's of lower version update rpm or if it's not available install it.
pkg=($@)
vinfo=($(rpm -q --qf "%{VERSION}-%{RELEASE}.%{ARCH} " ${pkg[0]} 2>&1))
if [ $? -eq 0 ]
then
need_upgrade=1
for vrs in ${vinfo[@]}
do
if [[ "${pkg[1]}-${pkg[2]}" = "$vrs" ]]
then
need_upgrade=0
elif [[ "${pkg[1]}-${pkg[2]}" < "$vrs" ]]
then
need_upgrade=0
fi
done
if [ $need_upgrade -eq 1 ]
then
rpm -Uvh "$PKG_DIR/${pkg[3]}" >> $LOGFILE 2>&1
rc=$?
fi
else
rpm -ivh "$PKG_DIR/${pkg[3]}" >> $LOGFILE 2>&1
rc=$?
fi
But the string comparison with < is comparing the strings lexicographically hence it's not working the way that I expect. In some cases, e.g. here exists a cpio of version 2.10-9.el6.x86_64. When it compares whether "2.10-11.el6_3.x86_64" < "2.10-9.el6.x86_64" the elif condition returns true hence it's not upgrading the packages.
Is there any other good approach to do this?