Bash: Nginx 版本检查切
Bash: Nginx Version check cut
我正在尝试检查安装的 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
此代码 'can' 有效,但我遇到了一个问题:
如果版本号更改,则剪切编号(本例中的 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
您可以使用正则表达式代替剪切。例如,要从 nginx-1.15.0
中提取版本号,请使用:
echo 'nginx-1.15.0' | grep -o '[0-9.]*$'
输出:1.15.0
结合此页面上的所有内容我得到:
nginx -v 2>&1 | awk -F' ' '{print }' | cut -d / -f 2
我正在尝试检查安装的 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
此代码 'can' 有效,但我遇到了一个问题:
如果版本号更改,则剪切编号(本例中的 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
您可以使用正则表达式代替剪切。例如,要从 nginx-1.15.0
中提取版本号,请使用:
echo 'nginx-1.15.0' | grep -o '[0-9.]*$'
输出:1.15.0
结合此页面上的所有内容我得到:
nginx -v 2>&1 | awk -F' ' '{print }' | cut -d / -f 2