我知道有几个帖子提出类似的事情,但没有一个解决我遇到的问题。
我正在处理一个脚本,该脚本处理与不同Bluetooth low energy
设备的连接,使用gatttool
从一些句柄读取,并使用这些值动态创建.json
文件。
我遇到的问题是gatttool
命令需要一段时间才能执行(由于device is busy
或类似的消息,并不总能成功连接到设备)。这些“错误”不仅会在错误的数据中转换为填充.json
文件,而且还允许脚本行继续写入文件(例如,添加额外的}
或类似文件)。我正在使用的命令的示例如下:
sudo gatttool -l high -b <MAC_ADDRESS> --char-read -a <#handle>
如何以可以等待某个输出的方式处理此问题?在这种情况下,--char-read
使用gatttool
时的理想输出为:
Characteristic value/description: some_hexadecimal_data`
这样我可以确保我逐行跟踪脚本,而不是让这些“跳转”。
答案 0 :(得分:3)
grep
可让您根据所需的数据过滤gatttool
的输出。
如果您实际上正在寻找等待直到遇到特定输出的方法,那么expect可能正是您要找的。< / p>
从联系手册:
expect [[-opts] pat1 body1] ... [-opts] patn [bodyn]
waits until one of the patterns matches the output of a spawned
process, a specified time period has passed, or an end-of-file is
seen. If the final body is empty, it may be omitted.