上下文
对于相当多的centos服务器我想安装一些监控软件,该软件是基于centos版本的,我想检查发布版本并在此基础上安装软件。
发行
似乎if语句在没有错误的情况下成功运行,而这两个或所有三个if语句的结果都不应该为true。我已经研究了if命令和测试,似乎应该在bash中使用双括号和single=symbol。我相信我做错了一件很简单的事,但我就是找不到。
代码

#!/bin/bash
versionknown=false
version=$(</etc/centos-release)
known_version1="CentOS release 6.9 (Final)"
known_version2="CentOS Linux release 7.3.1611 (Core)"

if [[ "$version"="CentOS release 6.9 (Final)" ]] ; then
    echo "Same versions as expected"
    versionknown=true
    #run commands for this specific version
fi

if [[ "$version"="CentOS Linux release 7.3.1611 (Core)" ]] ; then
    echo "Same versions as expected v2"
    versionknown=true
    #run commands for this specific version
fi


if [[ "$versionknown"=false ]] ; then
    echo "Different version detected than known:"
    echo $version
    echo "Aborted"
fi

echo $versionknown

结果
Same versions as expected
Same versions as expected v2
Different version detected than known:
CentOS Linux release 7.3.1611 (Core)
Aborted
true

更新
在得到一些响应之后,我修改了代码,在等号(=)周围添加了空格。仍然不能按预期工作,因为比较应该在第二个if语句上返回true,而不是。
代码2
#!/bin/bash
versionknown=false
version=$(</etc/centos-release)
known_version1="CentOS release 6.9 (Final)"
known_version2="CentOS Linux release 7.3.1611 (Core)"

if [[ "$version" = "CentOS release 6.9 (Final)" ]] ; then
        echo "Same versions as expected"
        versionknown=true
        #run script for this specific version
fi

if [[ "$version" = "CentOS Linux release 7.3.1611 (Core)" ]] ; then
        echo "Same versions as expected v2"
        versionknown=true
        #run script for this specific version
fi


if [[ "$versionknown" = false ]] ; then
        echo "Different version detected than known:"
        echo $version
        echo "Aborted"
fi

echo $versionknown

结果2
Different version detected than known:
CentOS Linux release 7.3.1611 (Core)
Aborted
false

更新
declare -p version了解到/etc/centos-release在脚本文件的末尾添加了一个空格,我相信在CentOS 6.9版(final)上不是这样的。在字符串中添加空间,或者使用已知的_版本变量将其全部添加到一起并添加空间可以解决问题,脚本现在可以按预期工作。

最佳答案

您需要在=符号周围加上空格;如果不加空格,shell将“expression”视为单个单词,而不是将两个单词进行比较以获得相等的结果。

[[ "$version" = "CentOS release 6.9 (Final)" ]]

[[ a=b ]]相当于[[ -n a=b ]],也就是说,您只需测试单个单词是否为非空字符串。
=中使用==还是[[ ... ]]是一个风格问题;bash同时支持这两种方式。
我将避免使用versionknown变量,并编写如下代码:
version=$(</etc/centos-release)
known_version1="CentOS release 6.9 (Final)"
known_version2="CentOS Linux release 7.3.1611 (Core)"

if [[ "$version" = "$known_version1" ]] ; then
    echo "Same versions as expected"
    #run commands for this specific version
elif [[ "$version" = "$known_version2" ]] ; then
    echo "Same versions as expected v2"
    #run commands for this specific version
else
    echo "Different version detected than known:"
    echo "$version"
    echo "Aborted"
fi

case语句
case $version in
   "$known_version1")
       echo "Same versions as expected"
       # ...
       ;;
   "$known_version2")
       echo "Same versions as expected v2"
       # ...
       ;;
   *)
      echo "Different version detected than known:"
      echo "$version"
      echo "Aborted"
esac

09-11 19:54
查看更多