bashkeyboardstdoutbarcodesigint

How to ask bash to wait for a result and send a SIGKILL when it get it?


I want to use zbarcam but after reading a barcode, it doesn't stop.

$ zbarcam | xvkbd -file - -window emacs
EAN-13:6941428130969
CODE-128:3096140900557

Do you know how I can tell bash to kill zbarcam after printing on the stdout the first \n ?


Solution

  • Try

    tmp=/tmp/barcode.$$ # Note: security risk
    zbarcam > $tmp &
    pid=$!
    # Sleep until file has content
    while [[ ! -s $tmp ]] ; do
        sleep 1
    done
    kill $pid
    cat $tmp
    

    Note that it might not work if zbarcam doesn't flush its output.