我知道有几个帖子问过类似的问题,但没有一个能解决我遇到的问题。
我正在编写一个脚本来处理与不同Bluetooth low energy
设备的连接,使用gatttool
从它们的一些句柄中读取并动态创建一个。 json
文件包含这些值。
我遇到的问题是 gatttool
命令需要一段时间才能执行(由于 device is busy
或类似情况,并不总是能成功连接到设备消息)。这些“错误”不仅转化为填充 .json
文件的错误数据,而且还允许脚本行继续写入文件(例如添加额外的 }
或相似的)。我正在使用的命令示例如下:
sudo gatttool -l high -b <MAC_ADDRESS> --char-read -a <#handle>
我怎样才能以等待特定输出的方式处理这个问题?在这种情况下,当您使用 gatttool
--char-read
时,理想的输出是:
Characteristic value/description: some_hexadecimal_data`
这样我就可以确保我是在逐行执行脚本,而不是有这些“跳跃”。
最佳答案
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.
关于bash:在继续之前等待特定的命令输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25480922/