I have a bash script that spawns another process which takes a long time to execute and often prints lots of error messages into its stdout without stopping.
I would like to read its stdout interactively and then stop it immediately upon matching an error.
Any ideas how to implement this?
For example in the script below I would like to stop the running ffmpeg upon seeing first "Error" in its stdout
#!/bin/bash FFMPEG="/usr/bin/ffmpeg" LIST=`find | grep \..` for i in $LIST; do OUTP="$i.txt" OUTP_OK="$i.txt.ok" TMP_OUTP="$i.tmp" if [ -f "$OUTP" -o -f "$OUTP_OK" ] ; then echo Skipping "$i" else echo Checking "$i"... RESULT="bad" echo "$FFMPEG" -v 5 -i "$i" -f null - 2> "$TMP_OUTP" "$FFMPEG" -v 5 -i "$i" -f null - 2> "$TMP_OUTP" && \ mv "$TMP_OUTP" "$OUTP" && \ RESULT=`grep -v "\(frame\)\|\(Press\)" "$OUTP" | grep "\["` if [ -z "$RESULT" ] ; then mv "$OUTP" "$OUTP_OK" fi fi done
ffmpeg output:
.... [eac3 @ 0x144a420] frame sync error Error while decoding stream #0:0 [eac3 @ 0x144a420] frame CRC mismatch Input stream #0:0 frame changed from rate:48000 fmt:s16 ch:6 to rate:16000 fmt:s16 ch:2 [eac3 @ 0x144a420] frame sync error Error while decoding stream #0:0 Input stream #0:0 frame changed from rate:16000 fmt:s16 ch:2 to rate:48000 fmt:s16 ch:6 [eac3 @ 0x144a420] frame sync error Error while decoding stream #0:0 [eac3 @ 0x144a420] frame sync error Error while decoding stream #0:0 [eac3 @ 0x144a420] frame sync error Error while decoding stream #0:0 [eac3 @ 0x144a420] frame CRC mismatch Input stream #0:0 frame changed from rate:48000 fmt:s16 ch:6 to rate:48000 fmt:s16 ch:2 [eac3 @ 0x144a420] frame sync error Error while decoding stream #0:0 Input stream #0:0 frame changed from rate:48000 fmt:s16 ch:2 to rate:48000 fmt:s16 ch:6 [eac3 @ 0x144a420] frame sync error Error while decoding stream #0:0 ...
...and on and on and on for 20 minutes...