【问题标题】:How do I negate two logic tests with regular expressions in a bash script?如何在 bash 脚本中使用正则表达式否定两个逻辑测试?
【发布时间】:2017-09-21 18:34:45
【问题描述】:

在bash中,我们想做如下逻辑:如果变量"$SLACK"TRUE并且${OUTPUT}不等于"mysql: [Warning] Using a password on the command line interface can be insecure",调用slack_it函数。

我已经尝试了How do I negate a test with regular expressions in a bash script?Check if a string matches a regex in Bash script 此处的帖子提供的十几种解决方案,但没有一个有效。有哪位大师能开导吗?

#!/bin/bash

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

【问题讨论】:

  • 我很好奇你为什么要使用正则表达式。听起来你让它在没有正则表达式的情况下工作。为什么要把事情复杂化?还是这是一种思维练习?

标签: regex bash if-statement


【解决方案1】:

星星需要在引号之外。

另外,=~ 运算符用于正则表达式,== 用于全局。

所以:

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

【讨论】:

    【解决方案2】:

    您的问题似乎与前导和尾随 * 有关。试试:

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

    【讨论】:

    猜你喜欢
    • 2011-05-31
    • 2010-09-23
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    相关资源
    最近更新 更多