3 min read

Tags

Last year, I wrote about an extended adb script. The idea of the script is to make it really easy to issue an adb command even if there are multiple devices attached by presenting a chooser. For example, if I have two physical devices and an emulator and I want to use my deeplink alias, I get presented with a device chooser:

โžœ  ~ deeplink https://zarah.dev
Multiple devices found:
1 - R5CR7039LBJ
2 - 39030FDJH01460
3 - emulator-5554
Select device: 

I wrote about this alias and how it works here.

What I eventually learned is that I cannot remember which of those devices is my test phone (which has the app that handles the deeplink) and which is my personal phone. ๐Ÿคฆโ€โ™€๏ธ It would be great if it also shows at least what kind of phone it is. Well, it turns out that adb devices can tell us this information! Hooray! The trick is to include the -l option:

โžœ  ~ adb devices -l
List of devices attached
R5CR7039LBJ            device usb:35926016X product:p3sxxx model:SM_G998B device:p3s transport_id:1
39030FDJH01460         device usb:34930688X product:shiba model:Pixel_8 device:shiba transport_id:1
emulator-5554          device product:sdk_gphone64_arm64 model:sdk_gphone64_arm64 device:emu64a transport_id:2

As before, letโ€™s find all valid devices, dropping any unauthorised ones, but this time letโ€™s grab all the information up to the model name:

valid_devices=$(echo "$all_devices" | grep -v "unauthorized" | grep -oE ".*?model:\S*")

At this point, the variable valid_devices contains the following:

R5CR7039LBJ            device usb:35926016X product:p3sxxx model:SM_G998B
39030FDJH01460         device usb:34930688X product:shiba model:Pixel_8
emulator-5554          device product:sdk_gphone64_arm64 model:sdk_gphone64_arm64

The only other update our existing script needs is to include the model name when the list of devices is displayed.

find_matches=$(echo "$valid_devices" | awk 'match($0, /model:/) {print NR " - " $1 " (" substr($0, RSTART+6) ")"}')

At the heart of it, what we need to do is extract pieces of information from each line; so awk should be good enough for this. When awk is invoked, it:

  • reads the input line by line
  • stores each line in a variable $0
  • splits each line into words
  • stores each word in variable from $1..$n

Thereโ€™s a lot of things happening in that awk command, so letโ€™s step through what it will do for each line in valid_devices:

match($0, /model:/)
  match built-in function that finds the first match of the provided regular expression
  $0 field variable containing the whole line
  /model:/ the regular expression to match ("model:"), awk syntax needs it to be inside slashes
print NR " - " $1 " (" substr($0, RSTART+6) ")"
  print prints the succeeding items concatenated with the designated separator (default is a space)
  NR the record number (i.e. line number being read)
  " - " print a literal space, a dash, and a space
  $1 field variable containing the first word
  " (" print a literal space and an open brace
  substr($0, RSTART+6)  
    substr built-in function to get a substring from $0, starting at index RSTART+6
    $0 field variable that contains the whole line
    RSTART the index of the last call to match
    +6 move the pointer six places (basically skip "model:")</code>
  ")"    print a literal closing brace

I found awk.js.org and jdoodle.com really helpful when playing around with awk. I found the explanations in awk.js.org particularly useful.

Running the deeplink alias again now shows the model name inside braces:

โžœ  ~ deeplink https://zarah.dev
Multiple devices found:
1 - R5CR7039LBJ (SM_G998B)
2 - 39030FDJH01460 (Pixel_8)
3 - emulator-5554 (sdk_gphone64_arm64)
Select device: 

Much better! ๐Ÿ‘Œ I just need to make sure I donโ€™t have to use more Samsungs cause I can never keep track of which Galaxy/Note/etc is which SM_. ๐Ÿ˜…

As always, the gist is in Github.