Casper
Casper

Reputation: 1723

Unix grep using Regex

In Unix, I need an input validation which use grep:

echo $INPUT| grep -E -q '^foo1foo2foo3' || echo "no"

What I really need is if input doesn't match at least one of these values: foo1 foo2 or foo3, exit the program.

Source: syntax is taken from Validating parameters to a Bash script

Upvotes: 0

Views: 276

Answers (3)

glenn jackman
glenn jackman

Reputation: 247230

Do you really need grep? If you're scripting in bash:

[[ $INPUT == @(foo1|foo2|foo3) ]] || echo "no"

or

[[ $INPUT == foo[123] ]] || echo "no"

If you want "$INPUT contains one of those patterns

[[ $INPUT == *@(foo1|foo2|foo3)* ]] || echo "no"

Upvotes: 2

Sasha Pachev
Sasha Pachev

Reputation: 5336

Does this solve your problem:

echo $INPUT | grep -E 'foo1|foo2|foo3' || echo "no"

?

Upvotes: 2

anubhava
anubhava

Reputation: 786349

You need to use alternation:

echo "$INPUT" | grep -Eq 'foo1|foo2|foo3' || echo "no"

Upvotes: 4

Related Questions