我该如何grep'hcitool lescan'的输出,或者将它通过管道传输到任何东西。似乎当我通过管道从“hcitool lescan”中获取任何内容时,都没有输出。
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
最佳答案
问题是标准输出缓冲。 'hcitool lescan'不会在找到每个新设备后刷新其输出,它只会使用'\n'打印它们(至少在我正在查看的bluez 5.27源码中)。默认情况下,如果stdout是终端,则缓冲将自动设置为“行缓冲”,否则将设置为缓冲(有关完整说明,请参见here)。
因此,例如,当您将hcitool的输出重定向到grep时,它将被缓冲。如果等待足够长的时间,则会看到grep的预期输出。
为了克服这个问题,您可以使用stdbuf来运行带有stdout行缓冲的hcitool:
$stdbuf -oL hcitool lescan | grep B
关于pipe - Grep Hcitool Lescan输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35895175/