Reputation: 600
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?
Upvotes: 0
Views: 104
Reputation: 1420
awk can be used to split out the 11 and 9 part from the input and then making comparisons.
This is the sample script:
version=`echo "2.10-11.el6_3.x86_64" | awk -F'.' '{print $2}' | awk -F'-' '{print
$2}'`
versiontocompare=`echo "2.10-9.el6_3.x86_64" | awk -F'.' '{print $2}' | awk -F'-'
'{print $2}'`
# version contains 11 now and versiontocompare contains 9
echo "$version $versiontocompare"
if [ $version -gt $versiontocompare ]
then
echo "Ok $version is greater than $versiontocompare. Do update"
else
echo "Do not update"
fi
Upvotes: 0
Reputation: 19315
sort -VC
, from man sort :
-V, --version-sort
natural sort of (version) numbers within text
-C, --check=quiet, --check=silent
like -c, but do not report first bad line
maybe
if sort -VC <<END
${pkg[1]}-${pkg[2]}
$vrs
END
then
Upvotes: 1