Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a Bash script which installs a customised 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.

share|improve this question
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

2 Answers 2

up vote 4 down vote accepted

You can do

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

I would use instead:

if [[ ! ( $1 =~ $instanceNameRegex || $1 =~ $secondInstanceNameRegex ) ]]; then
share|improve this answer

This might work for you:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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