Created
May 17, 2014 19:19
-
-
Save elsnosrap/06cb52a7a99b3f723c1e to your computer and use it in GitHub Desktop.
A useful shell script that wraps Android adb commands when multiple devices or emulators are connected. The script will prompt for a device or emulator to run the command against, if it detects multiple devices / emulators.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This is a wrapper for adb. If there are multiple devices / emulators, this script will prompt for which device to use | |
# Then it'll pass whatever commands to that specific device or emulator. | |
# Run adb devices once, in event adb hasn't been started yet | |
BLAH=$(adb devices) | |
# Grab the IDs of all the connected devices / emulators | |
IDS=($(adb devices | sed '1,1d' | sed '$d' | cut -f 1 | sort)) | |
NUMIDS=${#IDS[@]} | |
# Check for number of connected devices / emulators | |
if [[ 0 -eq "$NUMIDS" ]]; then | |
# No IDs, exit | |
echo "No emulators or devices detected - nothing to do." | |
exit 0; | |
elif [[ 1 -eq "$NUMIDS" ]]; then | |
# Just one device / emulator | |
adb $@ | |
exit 0; | |
fi | |
# If we got here, there are multiple devices, need to get information then prompt user for which device/emulator to uninstall from | |
# Grab the model name for each device / emulator | |
declare -a MODEL_NAMES | |
for (( x=0; x < $NUMIDS; x++ )); do | |
MODEL_NAMES[x]=$(adb devices | grep ${IDS[$x]} | cut -f 1 | xargs -I $ adb -s $ shell cat /system/build.prop | grep "ro.product.model" | cut -d "=" -f 2 | tr -d ' \r\t\n') | |
done | |
# Grab the platform version for each device / emulator | |
declare -a PLATFORM_VERSIONS | |
for (( x=0; x < $NUMIDS; x++ )); do | |
PLATFORM_VERSIONS[x]=$(adb devices | grep ${IDS[$x]} | cut -f 1 | xargs -I $ adb -s $ shell cat /system/build.prop | grep "ro.build.version.release" | cut -d "=" -f 2 | tr -d ' \r\t\n') | |
done | |
echo "Multiple devices detected, please select one" | |
for (( x=0; x < $NUMIDS; x++ )); do | |
echo -e "$[x+1]: ${IDS[x]}\t\t${PLATFORM_VERSIONS[x]}\t\t${MODEL_NAMES[x]}" | |
done | |
echo -n "> " | |
read USER_CHOICE | |
# Validate user entered a number | |
if [[ $USER_CHOICE =~ ^[0-9]+$ ]]; then | |
echo "executing following command:" | |
echo " adb -s ${IDS[$USER_CHOICE-1]} $@" | |
adb -s ${IDS[$USER_CHOICE-1]} $@ | |
else | |
echo "You must enter a number" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot for sharing the code.
How about restricting the USER_CHOICE to be in the range of [1, NUMIDS] ?
if [[ $USER_CHOICE =~ ^[0-9]+$ ]] && [[ $USER_CHOICE -le $NUMIDS ]] && [[ $USER_CHOICE -gt 0 ]]; then