Skip to content

Instantly share code, notes, and snippets.

@itzikBraun
Created July 18, 2016 17:52
Show Gist options
  • Save itzikBraun/698ff328f734e65829e19bf8bca410cc to your computer and use it in GitHub Desktop.
Save itzikBraun/698ff328f734e65829e19bf8bca410cc to your computer and use it in GitHub Desktop.
# pass an int as the index of the wanted devices as first parameter
# i.e adb-id.sh 1
# pass -f so the text copied to clipboard will be pre-fixed with adb -s
# It is possible to combine both parameters, i.e adb-id.sh 1 -f
IDS=$(adb devices | grep "[0-9]" | tr -s "\t " " " | cut -d " " -f 1)
# If $1 is a number we use it instead of reading.
re='^[0-9]+$'
if ! [[ $1 =~ $re ]] ; then
COUNT=0
for DEVICE in $IDS
do
echo "[$COUNT]" "$DEVICE" | tr -d '[:cntrl:]'
echo
let COUNT+=1
done
read -r id
else
id=$1
shift
fi
COUNT=0
for DEVICE in $IDS
do
if [[ "$COUNT" -eq "$id" ]]; then
SELECTED="$DEVICE"
fi
let COUNT+=1
done
if [[ $1 = "-f" ]]; then
echo "adb -s $SELECTED" | tr -d '[:cntrl:]' | pbcopy
else
echo $SELECTED | tr -d '[:cntrl:]' | pbcopy
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment