Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep hcitool lescan output

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
like image 975
mw. Avatar asked Mar 09 '16 15:03

mw.


2 Answers

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

like image 125
Yonatan Y Avatar answered Oct 20 '22 07:10

Yonatan Y


make sure to run with sudo:

$ sudo stdbuf -oL hcitool lescan | grep <pattern>
like image 1
kaptan Avatar answered Oct 20 '22 09:10

kaptan