user1656357
user1656357

Reputation: 21

Regular expression alteration not matching in BASH

I have been trying to get multiple not matching alteration to work in a BASH.

This is what I am trying to not match. If there are two parameters and a switch(-a,-b,-c,-d) is the first parameter.

Example:
./scriptname -a filename
./scriptname -d filename

What I want this to echo success is for:
./scriptname filename ipaddress.

The code that works is :

if [[ "$#" = "2" && "$1" =~ ([^-a][^-b][^-c]) ]]
    then
        echo "success"
    else
        echo "fail"
  fi

If I try to expand on the alteration with ([^-a][^-b][^-c][^-d]) it stops working. I have tried multiple syntax variants and nothing seems to work. I also tried to group them together like:

if [[ "$#" = "2" && "$1" =~ ([^-a][^-b]) && "$1" =~ ([^-c][^-d]) ]] and this fails as well.

Upvotes: 1

Views: 364

Answers (1)

Stephane Rouberol
Stephane Rouberol

Reputation: 4384

What about:

if [[ "$#" = "2" && "$1" =~ -[a-d]$ ]]

Upvotes: 2

Related Questions