如何在bash脚本中使用正则表达式否定两个逻辑测试?

时间:2017-04-24 22:51:42

标签: regex bash if-statement

在bash中,我们希望执行以下逻辑:如果变量"$SLACK"TRUE${OUTPUT}不等于"mysql: [Warning] Using a password on the command line interface can be insecure",请调用{{1功能。

我已尝试过How do I negate a test with regular expressions in a bash script?Check if a string matches a regex in Bash script中的帖子提供的十几个解决方案,但这些解决方案都不起作用。任何古茹都可以开导吗?

slack_it

2 个答案:

答案 0 :(得分:3)

星星需要在引号之外。

此外,=~运算符用于正则表达式,==用于globs。

所以:

if ! [[ ${OUTPUT} == *"mysql: [Warning] Using a password on the command line interface can be insecure"* ]]; then
    slack_it
fi

答案 1 :(得分:1)

您的问题似乎与前导*有关。尝试:

if ! [[ ${OUTPUT} =~ "mysql: [Warning] Using a password on the command line interface can be insecure" ]]; then
  slack_it
fi