Grep hcitool lescan output
Asked Answered
S

2

10

How do I grep the output of 'hcitool lescan' or for that matter pipe it to anything. It seems when I pipe anything from 'hcitool lescan' I get no output.

root@edison:/mnt/rtd# hcitool lescan |grep B

^Croot@edison:/mnt/rtd# hcitool lescan | tee foo

^Croot@edison:/mnt/rtd# hcitool lescan
LE Scan ...
B0:B4:48:xx:xx:xx (unknown)
B0:B4:48:xx:xx:xx xxxxxxxx
B0:B4:48:yy:yy:yy (unknown)
B0:B4:48:yy:yy:yy yyyyyyyy
Subtend answered 9/3, 2016 at 15:2 Comment(0)
P
11

The problem is stdout buffering. 'hcitool lescan' does not flush its output after every new found device, it just prints them with '\n' (at least in bluez 5.27 sources which I'm looking at). By default if stdout is a terminal then buffering is automatically set to 'line buffered', else it is set to buffered (see here for full description). Hence when you redirect the output of hcitool to grep for example, it is buffered. If you wait long enough, you would see the expected output from grep. To overcome this you can use stdbuf to run hcitool with stdout line-buffering:

$stdbuf -oL hcitool lescan | grep B

Puttier answered 10/3, 2016 at 9:29 Comment(1)
Unfortunately I don't seem to have stdbuf on the intel edision/yocto coreutilities, and have not figured out where to find them.Subtend
A
2

make sure to run with sudo:

$ sudo stdbuf -oL hcitool lescan | grep <pattern>
Aoristic answered 18/5, 2017 at 21:32 Comment(1)
unrelated to OP's question but I found it useful to install moreutils package and use ts command to timestamp each entry: sudo stdbuf -oL hcitool lescan --duplicates | ts | grep <pattern>Aoristic

© 2022 - 2024 — McMap. All rights reserved.