当比较特定字符串时如果条件不匹配则 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/