With the following yum info call we can capture the installed version of ambari-metrics-monitor

 yum info ambari-metrics-monitor| grep -i version |head -1
 Version     :

and to verify from output the version is,, I simply do:

 yum info ambari-metrics-monitor| grep -i version |head -1 |  grep ""

My feeling is that this way isn't the right way to match the version float number.

So what is the right way to match the version float number from output (according to my example)?

current_version="$( yum info $package | awk -F: '/Version/ {print $2}' )"

if [[ "$current_version" == "$required_version" ]]; then
    echo "Good to go"
    echo "Version mismatch - version $current_version is installed for $package"

With single awk command:

yum info ambari-metrics-monitor \
| awk -v ver="" '$1 ~ /[vV]ersion$/ && $3 == ver{ f=1; exit }
                        END{ printf "Version %smatched\n", (f? "" : "not ") }'

The output will be either Version matched or Version not matched


If the package is already installed, you can use the --queryformat argument of rpm to only print the version number, and then use the -F and -x arguments of grep to conveniently do an exact match:

rpm -q --queryformat %{VERSION} ambari-metrics-monitor \
| grep -qFx 2.6.10 && echo "version matched"

With just awk you could do something like:

yum info ambari-metrics-monitor \
| awk -F: '/Version/{ if ($2 == "") {print "yes"} else exit 1}'

Note on my yum there is whitespace before the version number so I have had to add it to the check string in order to match.

This will print yes (and exit 0) if it matches or exit with 1 if it doesn't

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.