vendredi 2 août 2019

Mix of regex and non-regex in bash if-statement

Inside of my $foo variable I have this data (please pay close attention to the .s and ,s):

,example.com,de.wikipedia.org,reddit,stackoverflow.com.,amazon.,

I am trying to write an if statement in bash that basically works like this:

if [[ "${foo}" =~ *','[a-z0-9]','* || "${foo}" =~ *','[a-z0-9]'.,'* ]]; then
    echo "Invalid input detected"
else
    echo "OK"
fi

I would like it to echo Invalid input detected since reddit and amazon. are in $foo. If I change the contents of $foo to be:

,example.com,de.wikipedia.org,www.reddit.com,stackoverflow.com.,amazon.com,

Then it would echo OK.

I am using bash 3.2.57(1)-release on macOS Yosemite.

Aucun commentaire:

Enregistrer un commentaire