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,663
  • 7
  • 35
  • 62

3 Answers3

4

You need to use alternation:

echo "$INPUT" | grep -Eq 'foo1|foo2|foo3' || echo "no"
anubhava
  • 761,203
  • 64
  • 569
  • 643
2

Does this solve your problem:

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

?

Sasha Pachev
  • 5,162
  • 3
  • 20
  • 20
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
  • 238,783
  • 38
  • 220
  • 352
  • 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