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.

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

2 Answers 2

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .