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.
Best Answer
Yes, you can do it with
grep -vz
which tellsgrep
to find lines that don't match the pattern you give it (-v
) and to read the entire ionput at once (z
), so that one match means the whole thing fails:So, in your case, something like:
If you want to avoid reading the whole output, just use another tool. Perl, for example: