Score:0

Get first output from STDOUT and terminate process

hn flag

I want to scan a single QR-code and return the result to STDOUT. I discovered ZBar, which is a nice little program that almost does everything I need.

$ zbarcam --raw

scans QR-codes with the PC camera and returns the data to STDOUT. However, it does not terminate. The program scans multiple codes and returns them. It only stops by closing the video window or sending an interrupt.

How do I in general only get the first output from STDOUT, terminate the process, and then send the received output to STDOUT again in the command line? I probably need a Bash script for that right?

bac0n avatar
cn flag
doesn't `-1, --oneshot` option terminate zbar?
trizuz avatar
hn flag
There is not an option like this.
bac0n avatar
cn flag
seems to be introduced in [0.23.1](https://github.com/mchehab/zbar/releases/tag/0.23.1)
Score:0
cn flag

Using bash, and assuming there is only one line to read (the qrcode does not have embedded newlines), you can try

(echo $BASHPID; exec zbarcam --raw ) | 
(read pid; IFS= read -r line; kill -hup "$pid"; printf "%s\n" "$line")

The $BASHPID is because $$ does not change in subshells, so is the parent shell pid. The IFS= read -r should retain backslash and leading spaces in the input.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.