Bash:Nginx版本检查

前端之家收集整理的这篇文章主要介绍了Bash:Nginx版本检查 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试检查安装的Nginx版本是否与配置文件中定义的版本相同.

我的代码

#check version

command="Nginx -v"
Nginxv=$( ${command} 2>&1 )
Nginxvcutted="echo ${Nginxv:21}"
Nginxonpc=$( ${Nginxvcutted} 2>&1 )

if [ $Nginxonpc != ${Nginx_VERSION} ]; then
  echo "${error} The installed Nginx Version $Nginxonpc is DIFFERENT with the Nginx Version ${Nginx_VERSION} defined in the config!"
else
    echo "${ok} The Nginx Version $Nginxonpc is equal with the Nginx Version ${Nginx_VERSION} defined in the config!"
fi

这段代码可以工作,但是我遇到了一个问题:
如果版本号更改,则剪切号(在此示例中为Nginxv:21)不再适合.

例:

Nginx-1.13.12 vs Nginx-1.15.0 (13 vs 14 chars)

有什么办法可以使它正常工作,而不会造成麻烦?

解:
我改编了@Mohammad Saleh Dehghanpour的解决方案,它的工作就像一个魅力:

command="Nginx -v"
Nginxv=$( ${command} 2>&1 )
Nginxlocal=$(echo $Nginxv | grep -o '[0-9.]*$')

echo $Nginxlocal
1.15.0
最佳答案
您可以使用正则表达式而不是cut.例如,要从Nginx-1.15.0中提取版本号,请使用:

回声’Nginx-1.15.0’| grep -o'[0-9.] * $’

输出:1.15.0

原文链接:https://www.f2er.com/nginx/532359.html

猜你在找的Nginx相关文章