linux - 如果比较特定字符串时条件不匹配,则 Bash

标签 linux bash shell

当比较特定字符串时如果条件不匹配则 Bash

匹配字符串:

Red Hat Enterprise Linux Server release 7.2 (Maipo)

代码:

machineOSVersion="Red Hat Enterprise Linux Server release 7.2 (Maipo)"

modifiedOSVersion="CentOS Linux release 7 OR Red Hat Enterprise Linux Server release 7.2 (Maipo)"

if [[ ${machineOSVersion} = *"${modifiedOSVersion}"* ]]; then

    echo -e "match"

else

    echo -e "doesn't match"

fi

我希望这会匹配,但它不会。

相同的代码适用于其他字符串。这是因为字符串中的 () 字符而失败吗?

最佳答案

你在比较中得到了 2 个变量。您必须按如下方式编写条件:

if [[ ${modifiedOSVersion} = *"${machineOSVersion}"* ]]; then

您可以这样想:${modifiedOSVersion} 是两个字符串中最大的一个,因此您需要向 ${machineOSVersion} 添加内容以匹配它。这些额外的内容在这里由两个 * 表示。

关于linux - 如果比较特定字符串时条件不匹配,则 Bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39397228/

相关文章:

linux - 如何从 bash 脚本中退出 yarn top 命令的交互模式

linux - Bash - 使用字符串变量创建字符串

bash - 菜单不想使用功能

linux - ssh 远程命令源交互式启动脚本

Python运行shell命令并模拟用户输入

c - 用于 C 的良好 SMTP + SSL/TLS 库?

linux - bash 脚本与 SSH 控制台不同

linux - 如何将标准错误流输出分配给bash中的变量?

bash - 在bash中,有没有办法在变量中替换或嵌套变量?

linux - 如何将模块安装到 Linux 内核 2.6.29