Linux

如何匹配輸出中的版本號

  • March 2, 2018

通過以下yum info呼叫,我們可以擷取已安裝的版本ambari-metrics-monitor

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

並從輸出驗證版本是2.6.1.0,我只是這樣做:

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

我的感覺是這種方式不是匹配版本浮點數的正確方式。

那麼從輸出中匹配版本浮點數的正確方法是什麼(根據我的範例)?

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

使用單個**awk**命令:

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 ") }'

輸出將是Version matchedVersion not matched

引用自:https://unix.stackexchange.com/questions/425520