![](/img/trans.png)
[英]one particular command in bash script doesn't work when run by cron, all other commands do
[英]Bash if condition doesn't match when comparing particular string
如果在比较特定字符串时条件不匹配,则执行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}
添加内容以使其匹配。 这些额外的东西在这里由两个*
。
您已将变量匹配反向。 您必须在子集变量而不是超集变量周围使用*
匹配。
采用:
[[ $modifiedOSVersion == *"$machineOSVersion"* ]] && echo "matched" || echo "nope"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.