i need to find if the system has the latest version of a particular package and install if it dosent. how do i compare the versions?
assume that i use rpm to find the installed version and a separate file has info of the required(latest) version.
how do i do it ?
ripper 0 Newbie Poster
Joshua Gourgues 1 Light Poster
What program are you tryin to update the revision for?
ripper 0 Newbie Poster
What program are you tryin to update the revision for?
php,mysql and apache
masijade 1,351 Industrious Poster Team Colleague Featured Poster
It depends, on Linux play around for a while with rpm -qa, on Solaris play around with pkginfo, on AIX, HP, and SCO (as well as the FreeBSD/NetBSD deriviates) I can't really help you. I don't remember there software management commands.
But as you can see, it depends on your operating system. It also depends on whether the packaging system was used at all, or if someone downloaded the source and compiled it, or maybe just a tar file version of a binary program.
ajcamp 0 Newbie Poster
Hi,
This may be your lucky day. I searched all over the internet and couldn't find an RPM compare program - you'd think there would be hundreds of them....So I wrote my own. Not elegant, but very functional. The key to getting useful information from rpm is using query formats --qf
The guts of this script is
# rpm -q -a --qf '%{NAME}&-%{VERSION}&-%{RELEASE}&(%{ARCH})
This script is probably real close to what you're wanting to do. It compares the installed RPMS against a -r requirements file. This script can show you how to parse off the version number from a RPM
so you can compare it against your requirements file.
# <this script> -c <fn> will generate a list of RPMS installed
# <this script> -r <fn> will compare against a requirements file
#!/bin/bash
# Andrew Campagnola
# This script will compare installed RPMs against a list of required RPMS
# It will also compare and list the versions of the RPMs
# ===============================================
# Options
# -a Report ALL RPMs - found, missing or mismatched versions
# -v Report missing and mismatched versions
# -m Only report missing RPMS
# -c Create a local RPM list file
usage="USAGE: rpmcompare [camvVh?:ri] \n-a(ALL) -m(MISSING) -v(MISMATCHED VERSION) -r <required rpms file> -i <installed RPMs file> -c <create local list> -V Verbose"
let verbose=0
let pause_mode=1
while getopts "Vh?i:r:c:avmp" options
do
case $options in
a ) report="all"
;;
c ) report="make_local_list"
catalog_file=$OPTARG
;;
v ) report="version"
;;
m ) report="missing"
;;
V ) let verbose=1
;;
p ) let pause_mode=1
;;
r ) required_file=$OPTARG
;;
i ) installed_file=$OPTARG
;;
h ) echo -e $usage
exit 1
;;
? ) echo -e $usage
exit 1
;;
* ) echo -e $usage
exit 1
;;
esac
done
if [ ! -n "$1" ]
then
echo -e $usage
exit 1
fi
echo -e "------------------------------\nReport Type = $report \nVerbose=$verbose \nReq's File: $required_file \nInstalled File: $installed_file\nPause Mode: $pause_mode\n------------------------------"
#
# -c Make a Local RPM Listing File (./local_installed_rpms)
#
if [ "$report" = "make_local_list" ] ; then
## rpm -q --qf '%{NAME}-%{VERSION}-%{RELEASE}(%{ARCH})(%{GROUP})\n' $var
rpm -q -a --qf '%{NAME}&-%{VERSION}&-%{RELEASE}&(%{ARCH})\n' > temp1
cat temp1 | tr ' ' '\n'| sort > temp2
cp temp2 $catalog_file
rm -f temp1 temp2
echo -e "Completed writing RPM catalog $catalog_file"
exit 0
fi
#
# Get required rpms list file (Remote Computer)
#
until test -f "$required_file"
do
echo "Can't find required file ("$required_file") - I need a file with the list of required RPMS: "
read required_file
done
#
# Got name of req file, now load it
#
echo "Reading Remote RPMS List from: < $required_file >"
remote_rpms=`cat $required_file | sort`
#
# Check for -i (Local Computer) List Of Installed RPMS
# If not supplied, a list will be generated using `rpm -qa`
#
if [ -s "$installed_file" ]
then
installed_rpms=`cat $installed_file`
echo -e "Reading Local Installed RPMS List from: < $installed_file >"
else
echo "No local catalog file specified: Querying (Local) installed RPMS"
## ? installed_rpms=`rpm -q --qf "\%{NAME}-\%{VERSION}-\%{RELEASE}(\%{ARCH})\n"`
rpm -q -a --qf '%{NAME}&-%{VERSION}&-%{RELEASE}&(%{ARCH})\n' > temp1
cat temp1 | tr ' ' '\n'| sort > temp2
installed_rpms=`cat temp2`
rm -f temp1 temp2
fi
echo "================================================================"
for check in $remote_rpms
do
let rpm_base_match=0
# c_base=${check%%-[0-9]*} # don't trim nums they can be part of name
c_base=${check%%&*}
# Trim Off Archecture from right of version
# c_arch="(${check##*(}"
c_arch=${check##*&}
c_version=${check%%&*}
#
# Trim base name from left of version
c_version=${c_version#*&}
if [ "$verbose" = "1" ] ; then echo -e "Checking For Required RPM: \nName: $c_base \nArch: $c_arch\nVer: $c_version" ; fi
#
# Check installed local RPMS for a match to $c_base
#
for installed in $installed_rpms
do
let rpm_base_match=0
i_base=${installed%%&*}
#
# Trim Off Archecture from right of version to (
#
i_arch=${installed##*&}
i_version=${installed%%&*}
#
# Trim base name from left of version
i_version=${i_version#*&}
# echo -e "Raw check var:" $check
# echo -e "Raw installed var:" $installed
#
# Test Names -------------------------
#
if [ "$c_base" != "$i_base" ]
then
continue # skip to next installed
fi
# name matched - descend here
#
if [ "$c_arch" = "$i_arch" ]
then
rpm_base_match=1
if [ "$report" = "all" ]
then
echo -e "\nNames MATCH: $c_base $c_arch"
fi
#
# Name Match, Test Versions
#
if [ "$i_version" = "$c_version" ]
then
echo -e "Versions MATCH\n(Req)$required_file: $c_version $c_arch\n(Local)$installed_file: $i_version $i_arch"
else # Versions Mis-Match
echo -e "Versions MIS-MATCH\n(Req)$required_file: $c_version $c_arch\n(Local)$installed_file: $i_version $i_arch"
if [ "$pause_mode" = "1" ]
then
echo "Paused: Press Enter To Continue"
read dummy
fi
fi # if versions don't match
break
fi # if arch match
done # next $installed in installed_rpms
if [ "$rpm_base_match" = "0" ]
then
echo -e "----------------------------"
echo -e "Missing RPM: \nName: $c_base \nArch: $c_arch\nVer: $c_version"
if [ "$pause_mode" = "1" ]
then
echo "Paused: Press Enter To Continue"
read dummy
fi
fi # if versions don't match
if [ "$verbose" = "1" ]
then
echo -e "----------------------------"
fi
done # next $check in check_rpms
unset installed_rpms
unset check_rpms
exit 0
Edited by TrustyTony because: fixed formatting
ajcamp 0 Newbie Poster
By the way, I'd be
VERY INTERESTED IN IMPROVEMENTS TO THIS SCRIPT.
Thanks
AJ
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.