1

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     : 2.6.1.0

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

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

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)?

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

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

With single awk command:

yum info ambari-metrics-monitor \
| awk -v ver="2.6.1.0" '$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

1

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"
0

With just awk you could do something like:

yum info ambari-metrics-monitor \
| awk -F: '/Version/{ if ($2 == " 2.6.1.0") {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.