Why does ADB commands break a bash script loop?

后端 未结 2 746
遇见更好的自我
遇见更好的自我 2020-12-06 18:21

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.

相关标签:
2条回答
  • 2020-12-06 18:49

    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.

    0 讨论(0)
  • 2020-12-06 19:11

    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)
    
    0 讨论(0)
提交回复
热议问题