Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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>

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

Can this be done with out temporary files and without a loop with sleep 1?

share|improve this question
up vote 3 down vote accepted

Your first attempt does work:

tail -f temp.file | while read LINE; do firefox $LINE; done

Then in another terminal, append to the temp file so that tail -f prints a line to STDOUT:

echo 'google.com' >> temp.file

Every time you do this, firefox will open a new tab.

share|improve this answer
Well, that's really strange. I was fighting like 2 or 3 hours with this problem. And now it simply works as expected... :) One hint for others: A firefox process should be running already! – lumbric Oct 11 '12 at 12:18
Since you say zbar --raw does not terminate, this sounds like everything you wanted. Is that so? Anyway, it's best to also quote $LINE, so urls with ampersands don't break the loop. – lynxlynxlynx Oct 13 '12 at 19:12

After trying around a bit more, I've found a partial solution:

zbarcam --raw|xargs -L1 firefox

The crucial part is the parameter -L1, which tells xargs to read only one single line. After reading the first QR code it runs firefox. zbar then still runs, but I can't scan a second QR code (xargs has to be called a second time somehow).

share|improve this answer

very, very close:

zbarcam --raw | xargs -n 1 chromium-browser

This works beautifully for me, every scan ends up in a new tab.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.