简体   繁体   English

用字符串bash比较变量

[英]Comparing variables with strings bash

Context 语境

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. 对于很多我们的CentOS服务器,我想安装一些监视软件,该软件基于CentOS版本,我想检查发行版本并基于该版本安装软件。

Issue 问题

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. 似乎if语句成功运行且没有错误,而对于两个或所有三个if语句,结果永远都不应该为真。 I've looked into the if commands and the tests, it seems that I should use double brackets and single = symbol in bash. 我研究了if命令和测试,看来我应该在bash中使用双括号和single =符号。 I believe that I'm doing something really simple wrong, but I just can't find it. 我相信我在做一些非常简单的错误,但是我找不到。

Code

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

Results 结果

Same versions as expected
Same versions as expected v2
Different version detected than known:
CentOS Linux release 7.3.1611 (Core)
Aborted
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. 仍然无法按预期工作,因为比较应该在第二个if语句中返回true,否则返回true。

Code #2 代码#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

Results #2 结果#2

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

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. declare -p version告诉我/etc/centos-release在脚本文件的末尾添加了一个空格,我相信在CentOS 6.9版(最终版)上不是这样。 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. 在字符串中添加空格,或者一起使用我的known_version变量并添加空格可以解决问题,脚本现在可以按预期运行。

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. 没有它,shell将“表达式”视为一个单词,而不是两个单词的相等性比较。

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

[[ a=b ]] is equivalent to [[ -na=b ]] , ie, you are simply testing if the single word is a non-empty string. [[ a=b ]]等效于[[ -na=b ]] ,即,您只是在测试单个单词是否为非空字符串。

Whether you use = or == inside [[ ... ]] is a matter of style; [[ ... ]]内部使用=还是==是一个风格问题; bash supports both. bash支持。

I would avoid using a versionknown variable altogether, and write the code like this: 我会避免完全使用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

or a case statement 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

As a general rule of thumb, on probably all of the programming languages, a single equal ( = ) sign is for assignment and not for equality check which in this case is double equal ( = ) sign. 根据一般经验,可能在所有编程语言中,单个等号( = )用于赋值,而不用于等价性检查,在这种情况下,等号是双等号( = )。

The reason you're entering all of your if statements is that the result of your assignment expression is true and therefore the if statement is being evaluated as true and being executed. 输入所有if语句的原因是赋值表达式的结果为true ,因此if语句被评估为true并正在执行。

Update 更新资料

Thanks for @chepner's comment, a single equal ( = ) sign is used for equality check when associated with single square brackets [ ] or double square brackets [[ ]] . 感谢@chepner的评论,当与单个方括号[ ]或双方括号[[ ]]关联时,将使用单个等号( = )进行相等性检查。 bash also supports double equal ( = ) sign as equality check in both cases [ ] or [[ ]] . bash在[ ][[ ]]两种情况下都支持双等号( = )作为相等性检查。

What you want is probably this: 您想要的可能是这样的:

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

You are doing something wrong in your if-statement. 您在if陈述中做错了什么。

Simple answer: 简单答案:

  1. Make sure to use the == if you want to compare strings. 如果要比较字符串,请确保使用==
  2. Please use if [ "$version" == "xyz" ] instead of if [[ "$version" = "xyz" ]] 请使用if [ "$version" == "xyz" ]而不是if [[ "$version" = "xyz" ]]

Summarized: if [ "$version"=="CentOS release 6.9 (Final)" ]; then 总结: if [ "$version"=="CentOS release 6.9 (Final)" ]; then if [ "$version"=="CentOS release 6.9 (Final)" ]; then and it sould work! if [ "$version"=="CentOS release 6.9 (Final)" ]; then ,它辛苦了!

Regards, FrankTheTank 问候,FrankTheTank

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM