Send the data back to the script that started the operation through the adb am start shell

I want to install a diagnostic application from adb and return data from it from a bash script. I know how to get started with adb , but I can’t find a way to return the data if, maybe, if I print to logcat and analyze the output, but it sounds like a hack. Is there any way to get data from the initiated action using adb ?

+6
source share
2 answers

If the data you want to send back to the script automation can be serialized into a string less than 4k long - using logcat is a natural choice. Just do your job to print the data in the log using Log.i("TAG", "DATA=" + your_data_string); and then use the following commands in your automation script to capture the output:

 # clear the logcat buffer adb logcat -c # start your activity adb shell am start <INTENT> # this line will block until a string with "TAG" tag and "Info" priority and # starting with "DATA=" is printed to the main log adb shell 'logcat -b main -v raw -s TAG:I | (grep -q ^DATA= && kill -2 $((BASHPID-1)))' # now you can capture the data and process it DATA=$(adb logcat -d -b main -v raw -s TAG:I | grep ^DATA=) 
+3
source

There is a little In / Out sample:

1st drug:

 tmpfile=$(mktemp /tmp/adb-XXXXXXXXX) exec 5> >(adb shell >$tmpfile) exec 6<$tmpfile rm $tmpfile 

Then you can write &5 and read from &6 :

 read -t .1 -u 6 foo;echo $foo shell@HWY625-U :/ $ read -t .1 -u 6 foo;echo $foo echo date >&5 read -t .1 -u 6 foo;echo $foo date read -t .1 -u 6 foo;echo $foo Sat Dec 17 18:20:35 CET 2016 

Nota: I rm $tmpfile for security reasons, but as long as the descriptors remain open, you can use them (the timeout read option works fine with the while ):

 >&5 echo uptime while read -t .1 -u 6 foo;do echo $foo;done uptime up time: 08:16:42, idle time: 01:20:19, sleep time: 06:47:13 

and

 ls -l $tmpfile ls: cannot access /tmp/adb-hbmJrFVX4: No such file or directory ls -l /proc/self/fd total 0 lrwx------ 1 user user 64 Dec 17 18:24 0 -> /dev/pts/8 l-wx------ 1 user user 64 Dec 17 18:24 1 -> pipe:[22316547] lrwx------ 1 user user 64 Dec 17 18:24 2 -> /dev/pts/8 l-wx------ 1 user user 64 Dec 17 18:24 5 -> pipe:[22316558] lr-x------ 1 user user 64 Dec 17 18:24 6 -> /tmp/adb-hbmJrFVX4 (deleted) 

When finished, close all:

 >&5 echo exit while read -t .1 -u 6 foo;do echo $foo;done exit exec 5>&- exec 6<&- 
0
source

All Articles