LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Compare installed RPM versions using Bash script (http://www.linuxquestions.org/questions/linux-newbie-8/compare-installed-rpm-versions-using-bash-script-276161/)

jimwelc 01-10-2005 01:25 PM

Compare installed RPM versions using Bash script
 
Hello All,

I am a newbie to the UNIX/Linux flavors and I am attempting to create an installer using RPM. Within my script I want to make sure that the systems has an installed vesion of RPM that is equal to or greater than a specified version. The below snippet gets the current version of RPM, but I am not sure how to parse the values to check for correctness.


#===========BEGIN Code snippet ==========

#============================================
f_Check_RPM_Version() # Checks the installed version of RPM
#============================================
{
ROOT_BIN_RPM="/bin/rpm"
USR_BIN_RPM="/usr/bin/rpm"
USR_LOCAL_BIN_RPM="/usr/local/bin/rpm"

#Checks to see if the RPM application
#is installed on the system
if [[ -e $ROOT_BIN_RPM || -e $USR_BIN_RPM|| -e $USR_LOCAL_BIN_RPM ]]; then
RPM_VERSION=`rpm -q rpm`

#****NEED to compare versions here *****
STR_BASE_ACCEPTABLE_VER="rpm-4.2-0.69"
#****NEED to compare versions here *****

printf "$STR_RPM_CORRECT_VERSION $RPM_VERSION"
else
printf "$STR_RPM_INCORRECT_VERSION"

exit 1
fi

} # end f_Check_RPM_Version()


#===========END Code snippet ==========



Any help you can proved would be greatly appreciated.

Best Regards,
-Jim

bulliver 01-10-2005 02:24 PM

Just clarification: You want to check if $RPM_VERSION is greater or equal to rpm-4.2-0.69?? Is that right?

jimwelc 01-10-2005 02:30 PM

RE: Compare installed RPM versions using Bash script
 
Correct. I am looking to check if $RPM_VERSION is greater or equal to rpm_4.2-0.69.


Thanks

bulliver 01-10-2005 02:49 PM

Note: this is untested...

Code:

#****NEED to compare versions here *****

STR_BASE_ACCEPTABLE_VER="rpm-4.2-0.69"

if  [[ "$RPM_VERSION" < "$STR_BASE_ACCEPTABLE_VER" ]]; then
    echo "$RPM_VERSION is too low..."
else
    echo "$RPM_VERSION is fine"
fi

#****NEED to compare versions here *****

Have a look at http://www.tldp.org/LDP/abs/html/comparison-ops.html for comparison operators in bash...

jimwelc 01-10-2005 03:41 PM

THANKS for your help bulliver. Your code snippet is just what I was looking for. In my attempt I was fudging up the '['. I will be sure to check out that link.

Best Regards,
-Jim

QICBoy 01-28-2005 10:31 AM

This is a string comparison, so it will fail on rpm_4.12-0.69. Isn't there a more reliable method, like using an rpm command?

jimwelc 01-28-2005 10:40 AM

Using your example....any built RPM has the following format of:

"[Product Name] - [Distribution Type] - [Major Version] . [Minor Version]. [Maintenance-Revision Version] - [Build Version].[Architecture]"

If this format is not followed then all bets are off. In my case what I ended up doing was stripping the [Major Version] . [Minor Version]. [Maintenance-Revision Version] - [Build Version] out of the string and then do a numerical comparison. Messy but I have not found any quick an easy way around this issue. If you come across anything please feel free to post it.

-Jim


All times are GMT -5. The time now is 11:17 PM.