0

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

Community
  • 1
  • 1
Casper
  • 1,585
  • 5
  • 31
  • 60

3 Answers3

4

You need to use alternation:

echo "$INPUT" | grep -Eq 'foo1|foo2|foo3' || echo "no"
anubhava
  • 713,503
  • 59
  • 514
  • 593
2

Does this solve your problem:

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

?

Sasha Pachev
  • 4,884
  • 3
  • 19
  • 19
2

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"
glenn jackman
  • 223,850
  • 36
  • 205
  • 328
  • Thank you, works great. I want to match exact so I'll use this `[[ $INPUT == foo[123] ]] || echo "no"` – Casper Jun 02 '16 at 21:31