用字符串bash比较变量 [英] Comparing variables with strings bash

查看:99
本文介绍了用字符串bash比较变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于很多我们的CentOS服务器,我想安装一些监视软件,该软件基于CentOS版本,我想检查发行版本并基于该版本安装软件.

For quite a lot of our CentOS servers I would like to install some monitoring software, the software is based on the CentOS version, I want to check the release version and install software based on that.

似乎if语句成功运行且没有错误,而对于两个或所有三个if语句,结果永远都不应该为真.我研究了if命令和测试,看来我应该在bash中使用双括号和single =符号.我相信我在做一些非常简单的错误,但是我找不到它.

It seems that the if statements are run successfully without errors while the results never should be true for both or all three if statements. I've looked into the if commands and the tests, it seems that I should use double brackets and single = symbol in bash. I believe that I'm doing something really simple wrong, but I just can't find it.

#!/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.

Update

After getting some responses I've changed my code, adding spaces around the equal signs(=). Still doesn't work as intended since the comparison should return true on the second if statement which it doesn't.

#!/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版(最终版)上不是这样.在字符串中添加空格,或者一起使用我的known_version变量并添加空格即可解决问题,脚本现在可以按预期工作.

Update

declare -p version learned me that /etc/centos-release has a space added to the end of the script file, I believe that on CentOS release 6.9 (Final) that wasn't the case. Adding the space in the string, or all together making use of my known_version variables and adding the space solves the issues, script now works as intended.

推荐答案

您需要在=符号周围使用空格;没有它,shell将表达式"视为一个单词,而不是两个单词的相等性比较.

You need whitespace around the = sign; without it, the shell treats the "expression" as a single word, not a comparison of two words for equality.

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

[[ a=b ]]等同于[[ -n a=b ]],即,您只是在测试单个单词是否为非空字符串.

[[ a=b ]] is equivalent to [[ -n a=b ]], i.e., you are simply testing if the single word is a non-empty string.

[[ ... ]]中使用=还是==是一个风格问题; bash都支持.

Whether you use = or == inside [[ ... ]] is a matter of style; bash supports both.

我将避免完全使用versionknown变量,并编写如下代码:

I would avoid using a versionknown variable altogether, and write the code like this:

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

这篇关于用字符串bash比较变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆