【问题标题】:Piping timeout cmd to grep needs root管道超时 cmd 到 grep 需要 root
【发布时间】:2017-06-16 22:27:09
【问题描述】:

这里是一个 bash 菜鸟。尝试为 Home Assistant 编写单行 BLE 标签存在检测器(RPI-3 上的内置 BLE 模块会显着减慢 Home Assistant)。

需要一些帮助才能完成这项工作:

$ timeout -s SIGINT 5s hcitool -i hci0 lescan | grep 00:00:00:00:00:00

正在发生的事情(或应该发生的事情):

  1. hcitool 运行 5 秒(5 秒后被优雅地杀死)
  2. 输出通过管道传送到 grep 以检测一个特定的 MAC
  3. 命令返回匹配的mac

但是,此命令不返回任何内容,只返回状态码 130 ($ echo $?)

但是这可行:

$ sudo timeout -s SIGINT 5s hcitool -i hci0 lescan | grep 00:00:00:00:00:00

是的,我已经设置了此线程中描述的缺失上限:https://unix.stackexchange.com/questions/96106/bluetooth-le-scan-as-non-root

由于设置的功能,这也可以正常工作(返回检测到的设备列表,在 5 秒后优雅地终止:

$ timeout -s SIGINT 5s hcitool -i hci0 lescan

我做错了什么?为什么似乎管道到 grep 需要 root? (因为我知道不应该)

最好的问候,

斯蒂芬

【问题讨论】:

    标签: grep timeout bluetooth-lowenergy raspberry-pi3 piping


    【解决方案1】:

    啊解决了;)

    似乎是执行顺序:)。使用 sudo 与使用相同的效果:

    $ (timeout -s SIGINT 5s hcitool -i hci0 lescan | grep 00:00:00:00:00:00)
    

    【讨论】: