Grep show all lines not just matches set exit status

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I m piping some output of a command to egrep, which I m using to make sure a particular failure string doesn t appear in.

The command itself, unfortunately, won t return a proper non-zero exit status on failure, that s why I m doing this.

command | egrep -i -v "badpattern"

This works as far as giving me the exit code I want (1 if badpattern appears in the output, 0 otherwise), BUT, it ll only output lines that don t match the pattern (as the -v switch was designed to do). For my needs, those lines are the most interesting lines.

Is there a way to have grep just blindly pass through all lines it gets as input, and just give me the exit code as appropriate?

If not, I was thinking I could just use perl -ne "print; exit 1 if /badpattern/". I use -n rather than -p because -p won t print the offending line (since it prints after running the one-liner). So, I use -n and call print myself, which at least gives me the first offending line, but then output (and execution) stops there, so I d have to do something like

perl -e $code = 0; while (<>) { print; $code = 1 if /badpattern/; } exit $code

which does the whole deal, but is a bit much, is there a simple command line switch for grep that will just do what I m looking for?

Answers

Actually, your perl idea is not bad. Try:

perl -pe  END { exit $status } $status=1 if /badpattern/; 

I bet this is at least as fast as the other options being suggested.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/6145452/grep-show-all-lines-not-just-matches-set-exit-status

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils