1

I have a Bash script which installs a customized JBoss. The name of the JBoss instance is checked against a regular expression to make sure that the name is valid:

if [[ ! $1 =~ $instanceNameRegex ]]; then
    exit 1
fi

We now have a requirement for a second valid regex. ie the name must match either $instanceNameRegex or $secondInstanceNameRegex.

How do I achieve this in the if test? I'm looking for this, but I can't get it to work:

if [[ [ ! $1 =~ $instanceNameRegex ] -a [ ! $1 =~ $secondInstanceNameRegex ] ]]; then

ie if the parameter doesn't match the first regex AND it doesn't match the second, then exit.

  • 1
    Since when has [ been an argument to [[? – Ignacio Vazquez-Abrams Feb 2 '12 at 9:59
  • 1
    That's why I'm asking on here: I'm a Java programmer! – Rich Feb 2 '12 at 10:04
4

You can do

if [[ ! $1 =~ $instanceNameRegex && ! $1 =~ $secondInstanceNameRegex ]]; then

I would use instead:

if [[ ! ( $1 =~ $instanceNameRegex || $1 =~ $secondInstanceNameRegex ) ]]; then
0

This might work for you:

if [[ ! $1 =~ "${instanceNameRegex}|${secondInstanceNameRegex}" ]]; then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.