See command line for line

(In bash) Is it possible to view the output of a command line program and a flag when a particular line occurs?

Basically my problem is that we have large build scripts that generate mountains of output to the console. I want to look at the console to say the word "ERROR", and then somehow warn me.

Then I know to go and see the rest of the process / cancel it.

+4
source share
3 answers

Try this from the command line:

<YOUR_BUILD_CMD> | grep ERROR 

it only issues ERROR when <YOUR_BUILD_CMD> displays it. | is pipe , and here is a guide for grep .

+5
source

You can grep for the string, but you will need to keep the original output of make, which can be done with tee . An example of using mplayer to play a sound file immediately after an error occurs:

 make 2>&1 | tee make.log | grep -lq ERROR && mplayer alert.wav 
+5
source

I would skip this through a while loop to check every line of output. eg,

 cmd &| while read line;do if [[ ${line} =~ ErrorRegex ]];then echo Error Detected: $line; else echo $line; fi;done 

to break it into "& |" redirects stderr and stdout to a while loop, which reads 1 line of input at a time and puts it in a string variable. then a simple regular expression comparison searches for a string with an error, and you can select that string or write it somewhere anywhere that you like, really go crazy. If it does not match the error, it simply prints the output as usual.

0
source

All Articles