JackWM
JackWM

Reputation: 10535

Bash: enumerate all the attached devices

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.

Upvotes: 4

Views: 2966

Answers (3)

jaypal singh
jaypal singh

Reputation: 77085

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')

Upvotes: 8

Chris Seymour
Chris Seymour

Reputation: 85775

You could use xargs and awk:

adb devices | awk 'NR>1{print $1}' | xargs -n1 -I% adb -s % install foo.apk

Demo:

I put your input into a file and using echo to check the ouput produces:

$ awk 'NR>1{print $1}' file | xargs -n1 -I% echo adb -s % install foo.apk
adb -s 015d4a826e0ffb0f install foo.apk
adb -s 015d4a826e43fb16 install foo.apk
adb -s 015d41d830240b11 install foo.apk
adb -s 015d2578a7280b02 install foo.apk

Upvotes: 3

Oleg S Kleshchook
Oleg S Kleshchook

Reputation: 464

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

Upvotes: 3

Related Questions