I\'m noticing a problem when running multiple adb commands from a shell script loop most of the commands does not execute.
This is an example script.
While @ChrisDodd's root cause analysis is correct and his solution works fine in general, for adb
automation purposes it is preferable to use adb exec-out
instead of suggested adb </dev/null shell
for simple adb shell commands.
Also why use grep
when you are already using awk
?
for device in $(adb devices | awk '$2=="device"{print$1}')
do
serialno=$(adb -s $device exec-out getprop ro.serialno)
appinstallcount=$(adb -s $device exec-out pm list packages | wc -l)
...
done
For adb exec-out
to work you need both your host PC and devices have somewhat recent adb
(or adbd
in case of devices) version - platform-tools
v24+ and Android 5.1+ should do.
adb shell
connects stdin to the command running on the device, which will generally consume stdin until a EOF is reached. So these commands consume all of the rest of your device names, causing the loop to exit.
Run adb
with a stdin redirection, so they get an immediate EOF without messing with what you're trying to loop over:
serialno=$(adb </dev/null -s $device shell getprop ro.serialno)
appinstallcount=$(adb </dev/null -s $device shell pm list packages | wc -l)