0

In bash, we would like to do the following logic: if the variable "$SLACK" is TRUE and ${OUTPUT} is not equal to "mysql: [Warning] Using a password on the command line interface can be insecure", call the slack_it function.

I have tried more than a dozen solutions provided from the posts here How do I negate a test with regular expressions in a bash script?, Check if a string matches a regex in Bash script, but none of them work. Could any guru enlighten?

#!/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
Community
  • 1
  • 1
Chubaka
  • 2,933
  • 7
  • 43
  • 58
  • I am curious why you are set on using a regular expression. It sounds like you got it to work without a regular expression. Why complicate things? Or is this a mind exercise? – Bill Rosmus Apr 24 '17 at 23:07

2 Answers2

3

The stars need to be outside the quotes.

Also, the =~ operator is for regex, == for globs.

So:

if ! [[ ${OUTPUT} == *"mysql: [Warning] Using a password on the command line interface can be insecure"* ]]; then
    slack_it
fi
wjandrea
  • 28,235
  • 9
  • 60
  • 81
1

Your issue seems to be related to the leading and trailing *. Try:

if ! [[ ${OUTPUT} =~ "mysql: [Warning] Using a password on the command line interface can be insecure" ]]; then
  slack_it
fi
codeforester
  • 39,467
  • 16
  • 112
  • 140
  • 1
    You also [can't use quotes when matching regular expressions in Bash](http://stackoverflow.com/q/218156/1426891), or it'll force string matching. Saving the regex to a variable seems to be the workaround of choice. – Jeff Bowman Apr 24 '17 at 22:56