Skip to content

Instantly share code, notes, and snippets.

@mckabue
Last active February 8, 2020 06:11
Show Gist options
  • Save mckabue/ac124f736085e9650fdbf63b1d9ca5ab to your computer and use it in GitHub Desktop.
Save mckabue/ac124f736085e9650fdbf63b1d9ca5ab to your computer and use it in GitHub Desktop.
This script allows to debug android with adb commands in terminal easily, with one or many devices/emulators: See blog at https://dev.to/mckabue/connect-debug-android-with-adb-android-debug-bridge-commands-in-terminal-easily-with-one-or-many-devices-emulators-1g1o
#!/bin/bash
devices=(`adb devices | grep -v devices | grep device | cut -f 1`)
devicesCount=${#devices[@]}
devicesToUse=()
if [ $devicesCount -eq 0 ]
then
echo "No attached devices.";
elif [ $devicesCount -eq 1 ]
then
name=`adb -s ${devices[0]} shell getprop ro.product.model`
api=`adb -s ${devices[0]} shell getprop ro.build.version.sdk`
version=`adb -s ${devices[0]} shell getprop ro.build.version.release`
echo "One attached device.";
read -p "Use \"$name [$version - API $api]\"?[yes/no] (default: yes): " useDevice
useDevice=${useDevice,,}
if [ "$useDevice" = "yes" ] || [ -z "$useDevice" ]
then
devicesToUse=(${devices[0]})
fi
else
echo "$devicesCount attached devices."
defaultDeviceName=`adb -s ${devices[0]} shell getprop ro.product.model`
defaultDeviceApi=`adb -s ${devices[0]} shell getprop ro.build.version.sdk`
defaultDeviceVersion=`adb -s ${devices[0]} shell getprop ro.build.version.release`
echo "Which device do you want to use? (default: \"$defaultDeviceName [$defaultDeviceVersion - API $defaultDeviceApi]\")"
for i in ${!devices[@]};
do
device=${devices[$i]}
name=`adb -s $device shell getprop ro.product.model`
api=`adb -s $device shell getprop ro.build.version.sdk`
version=`adb -s $device shell getprop ro.build.version.release`
echo "$(($i+1))) $name [$version - API $api]"
done
echo "$(($devicesCount+1))) all"
read -p "Choose a number: " deviceIndex
deviceIndex=$((${deviceIndex,,}-1))
if [ $deviceIndex -gt 0 ] && [ $deviceIndex -lt $devicesCount ];
then
devicesToUse=(${devices[$deviceIndex]})
elif [ $deviceIndex -eq $devicesCount ]
then
devicesToUse=("${devices[@]}")
else
devicesToUse=(${devices[0]})
fi
fi
devicesToUseCount=${#devicesToUse[@]}
if [ $devicesToUseCount -gt 0 ]
then
for i in ${!devicesToUse[@]};
do
device=${devicesToUse[$i]}
name=`adb -s $device shell getprop ro.product.model`
api=`adb -s $device shell getprop ro.build.version.sdk`
version=`adb -s $device shell getprop ro.build.version.release`
echo "Using \"$name [$version - API $api]\""
adb -s $device "$@"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment