Skip to main content
edited title
Link
Alexander Mills
  • 10.9k
  • 27
  • 120
  • 214

grep - exit with 1 - if match

Source Link
Alexander Mills
  • 10.9k
  • 27
  • 120
  • 214

grep exit with 1 if match

I have a bash function like so:

run_mongo(){ mongo foo bar baz 2>&1 # send stderr to stdout } 

unfortunately this mongo command doesn't exit with 1 if there is error, so I have to match on stdout/stderr

is there some way to exit with code > 0 with grep if there is a first match? something like this:

run_mongo | grep -e "if stdout/stderr matches this we exit with 1" 

I assume the way to do this would be like so:

run_mongo | grep -e "if stdout/stderr matches" | killer 

where killer is a program that dies as soon as it gets its first bit of stdin.