Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@intrications
Forked from dtmilano/android-select-device
Created January 21, 2013 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intrications/4589366 to your computer and use it in GitHub Desktop.
Save intrications/4589366 to your computer and use it in GitHub Desktop.
#! /bin/bash
#=====================================================================
# Selects an android device
# Copyright (C) 2012-2013 Diego Torres Milano. All rights reserved.
#=====================================================================
get_adb_devices()
{
adb devices 2>&1 | tail -n +2 | sed '/^$/d'
}
PROGNAME=$(basename $0)
UNAME=$(uname)
DEVICE_OPT=
for opt in "$@"
do
case "$opt" in
-d|-e|-s)
DEVICE_OPT=$opt
;;
start-server|kill-server|-help)
exit 0
;;
esac
done
[ -n "$DEVICE_OPT" ] && exit 0
#DEV=$(adb devices 2>&1 | tail -n +2 | sed '/^$/d')
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 coulnd'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