Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: wait for specific command output before continuing

I know there are several posts asking similar things, but none address the problem I'm having.

I'm working on a script that handles connections to different Bluetooth low energy devices, reads from some of their handles using gatttool and dynamically creates a .json file with those values.

The problem I'm having is that gatttool commands take a while to execute (and are not always successful in connecting to the devices due to device is busy or similar messages). These "errors" translate not only in wrong data to fill the .json file but they also allow lines of the script to continue writing to the file (e.g. adding extra } or similar). An example of the commands I'm using would be the following:

sudo gatttool -l high -b <MAC_ADDRESS> --char-read -a <#handle>

How can I approach this in a way that I can wait for a certain output? In this case, the ideal output when you --char-read using gatttool would be:

Characteristic value/description: some_hexadecimal_data`

This way I can make sure I am following the script line by line instead of having these "jumps".

like image 352
alvaro.delaserna Avatar asked Sep 30 '22 00:09

alvaro.delaserna


1 Answers

grep allows you to filter the output of gatttool for the data you are looking for.

If you are actually looking for a way to wait until a specific output is encountered before continuing, expect might be what you are looking for.

From the manpage:

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.
like image 70
DevSolar Avatar answered Oct 20 '22 15:10

DevSolar