I need to read lines from STDIN and process them. I could do something like:
<print lines to STDOUT> | while read line; do
<process $line>
done
But this means that first all lines are printed and piped and then they are processed. I want to process every single line immediately, eventually before the next lines are sent to STDOUT.
This might be useful if the lines should be processed while they are are generated (e.g. a log file). My task is to use zbar (warning! the web page crashes Firefox...) to scan several QR codes and to open its URLs in Firefox.
If run zbarcam --raw
the webcam is used to scan QR codes. If a QR code is found the URL is printed to STDOUT immediately, but zbarcam does not terminate. So I'd like to read this line and to run firefox $url
or so (but without waiting for other URLs).
I've found a solution, but I'm wondering if this can't be done easier. Basically he writes the output of zbarcam
to a temporary file and waits until something is written there:
zbarcam --raw > tmpfile &
...
while [[ ! -s $tmp ]] ; do
sleep 1
done
Can this be done with out temporary files and without a loop with sleep 1
?