ADB control script that simplifies working with multiple devices

Running ADB commands over the command line can be a pain if you have multiple devices or emulators attached. The script below makes this easier by adding an interactive prompt.

Usage

  1. Run any combination of arguments that you would otherwise pass on adb to the script instead, like so: ./adb.sh install -r my_app.apk.
  2. If you have multiple devices attached, the script will list them all and ask which one to run it on.
  3. Hit a number, the command will be executed on that device.
  4. For added convenience you can add an alias for this script, e.g. adb2 or adbplus, in your .bashrc or .bash_profile: alias adbplus=/path/to/script/adb.sh

Example:

./adb.sh install -r my_app.apk

Output:

Choose a device to execute this command on:
  adb install -r my_app.apk

  [1] Nexus 6P
  [2] Nexus 5
  [a] All
  [c] Cancel

>2
adb -s 168f8abc8af6 install -r my_app.apk
my_app.apk pushed. 5.9 MB/s (15416456 bytes in 2.505s)
  pkg: /data/local/tmp/my_app.apk
Success

Additional functionality

The script adds a shortcut command to simplify launching a url on your device. This is useful when testing deeplinking:

./adb.sh open https://pixplicity.com

The script

adb.sh

#!/bin/bash

COMMAND=$@

### Handle special simplified commands
if [ "$1" = "open" ]; then
  if [ -z "$2" ]; then
    echo "No url supplied for `open` command."
    exit 1
  else
    COMMAND="shell am start -a android.intent.action.VIEW -d '$2'"
  fi
fi

### Get list of only the device identifiers
# 1 list devices
# 2 grep away the line 'list of devices'
# 3 grep away newline at the end
# 4 cut off everything but the first part (identifier)
ALL=`adb devices -l`
#LIST=`adb devices | grep -v devices | grep device | cut -f1`
LIST=`echo "$ALL" | grep -v devices | grep device | awk '{print $1}'`

### Similar, but grabbing the model and brand instead
# 4 cut off everything but the last part ('model:HTC_One_X')
# 5 take only the part after the colon ('HTC_One_X')
DEVICES=`echo "$ALL" | grep -v devices | grep device | awk '{print $5}' | cut -d':' -f2`
# 6 replace underscores with spaces 
DEVICES=`echo "${DEVICES//_/ }"`

### Count lines
COUNT=`echo "$LIST" | wc -l`

### Execute command
if ((COUNT==0))
then
  echo "No devices connected."
  exit 1
elif ((COUNT==1))
then
  # execute regular command
  adb $COMMAND
else
  # Show chooser
  while true; do
    echo "Choose a device to execute this command on:"
    echo "  adb $COMMAND"
    echo ""
    i=1
    while read -r line; do
      echo "  [$i] $line"
      i=$((i+1))
    done <<< "$DEVICES"
    echo "  [a] All"
    echo "  [c] Cancel"
    echo ""
  
    read -n1 -s n
    if (( n <= COUNT && n > 0 )); then
      SERIAL=`echo "$LIST" | head -n $n | tail -n 1`
      echo "adb -s $SERIAL $COMMAND"
      adb -s $SERIAL $COMMAND
      break
    elif [[ $n == "c" ]]; then
      echo "Canceled."
      break
    elif [[ $n == "a" ]]; then
      for SERIAL in $LIST; do
        echo "adb -s $SERIAL $COMMAND"
        adb -s $SERIAL $COMMAND
      done
      break
    else
      echo "Let's try that again."
      continue
    fi
  done
fi