Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Script to select one connected device or emulator when running adb

View android-select-device
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#! /bin/bash
#=====================================================================
# Selects an android device
# Copyright (C) 2012-2013 Diego Torres Milano. All rights reserved.
#
# See:
# - http://dtmilano.blogspot.ca/2013/01/android-select-device.html
# - http://dtmilano.blogspot.ca/2012/03/selecting-adb-device.html
# for details on usage.
#=====================================================================
 
get_adb_devices()
{
adb devices 2>&1 | tail -n +2 | sed '/^$/d'
}
 
git_pull()
{
[[ "$UNAME" == 'Linux' ]] && OPT=-e
ASD=$(readlink $OPT $0)
if [[ -n "$ASD" ]]
then
DIR=$(dirname $ASD)
if [[ -n "$DIR" && -d "$DIR/.git" ]]
then
(cd $DIR && git pull)
fi
fi
}
 
PROGNAME=$(basename $0)
VERSION="3.0.2"
UNAME=$(uname)
DEVICE_OPT=
 
git_pull > /dev/null
 
for opt in "$@"
do
case "$opt" in
-d|-e|-s)
DEVICE_OPT=$opt
;;
start-server|kill-server|connect|-help)
exit 0
;;
 
-V|--version)
echo "$PROGNAME version $VERSION"
exit 0
;;
esac
done
[ -n "$DEVICE_OPT" ] && exit 0
DEV=$(get_adb_devices)
if [ -z "$DEV" ]
then
echo "$PROGNAME: ERROR: There's no connected devices." >&2
exit 1
elif echo "$DEV" | grep -q 'daemon started successfully'
then
# try again
DEV=$(get_adb_devices)
fi
N=$(echo "$DEV" | wc -l | sed 's/ //g')
 
case $N in
1)
# only one device detected
D=$DEV
;;
 
*)
# more than one device detected
OLDIFS=$IFS
IFS="
"
PS3="Select the device to use, <Q> to quit: "
select D in $DEV
do
[ "$REPLY" = 'q' -o "$REPLY" = 'Q' ] && exit 2
[ -n "$D" ] && break
done < /dev/tty
 
IFS=$OLDIFS
;;
esac
 
if [ -z "$D" ]
then
echo "$PROGNAME: ERROR: target device couldn't be determined" >&2
exit 1
fi
 
# this didn't work on Darwin
# echo "-s ${D%% *}"
echo "-s $(echo ${D} | sed 's/ .*$//')"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.