Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: enumerate all the attached devices

Tags:

android

bash

awk

I plugged several android devices to my laptop. And I can list their SN by

adb devices

output:

List of devices attached 
015d4a826e0ffb0f    device
015d4a826e43fb16    device
015d41d830240b11    device
015d2578a7280b02    device

I want to perform some operations on every device, like

adb -s $device install foo.apk

But I don't know how to let variable device iterate all the devices obtained by adb devices.

like image 873
JackWM Avatar asked Dec 11 '22 14:12

JackWM


2 Answers

One way to do it in bash. Read the output of your command and iterate it on the second column using a while loop.

while read sn device; do
    adb -s "$sn" install foo.apk
done < <(adb devices | sed '1d')
like image 153
jaypal singh Avatar answered Dec 21 '22 11:12

jaypal singh


Main trick is to separate serial of device from other output. You need to cut off header and second column. Something like this would work:

for DEVICE in `adb devices | grep -v "List" | awk '{print $1}'`
do 
  adb -s $DEVICE install foo.apk
done
like image 35
Oleg S Kleshchook Avatar answered Dec 21 '22 10:12

Oleg S Kleshchook