Bash: list all connected devices

I have connected several Android devices to my laptop. And I can list their SN on

adb devices

output:

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

I want to perform some operations on each device, for example

adb -s $device install foo.apk

But I do not know how to allow the variable device iterate all devices received with adb devices .

+4
source share
3 answers

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

 while read sn device; do adb -s "$sn" install foo.apk done < <(adb devices | sed '1d') 
+7
source

The main trick is to separate the serial number of the device from another output. You need to disable the header and second column. Something like this will work:

 for DEVICE in `adb devices | grep -v "List" | awk '{print $1}'` do adb -s $DEVICE install foo.apk done 
+3
source

You can use xargs and awk :

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

Demo:

I put your input in file and using echo to check the output:

 $ 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 
+2
source

Source: https://habr.com/ru/post/1485638/


All Articles