How to install new package version (update package) when there is new update available in repo using shell script

293 Views Asked by At

I need to write a shell script that upgrade a package by comparing installed and available version. For example git 2.3 is installed in my machine, I need to upgrade to new version (if available) through shell script.

I tried this:

#!/bin/bash
package_name=git
INSTALLED_VERSION= rpm -q $package_name
AVAILABLE_VERSION= yum --showduplicates list $package_name
echo $INSTALLED_VERSION
echo $AVAILABLE_VERSION

if [ $INSTALLED_VERSION -lt $AVAILABLE_VERSION ] ; then
        yum install -y  $package_name
fi

# check version:
git --version
1

There are 1 best solutions below

2
Diego Torres Milano On

yum can do it for you, just run

yum update -y "$package_name"

and it will update the package if a new version is available.