Home > database >  ADB devices - Get all IPs matching the device state
ADB devices - Get all IPs matching the device state

Time:11-24

When I issue command:

adb devices

I have result like this:

List of devices attached
192.168.1.200:5555      offline
192.168.1.201:5555      device
192.168.1.202:5555      unauthorized
192.168.1.203:5555      device

I have this bash script, but is failing cos it's working only on connected devices:

#!/bin/bash
#init
a=$(adb devices | cut -f1 | cut -f1 -d\ );
echo $a;
for x in $a;
do 
    if [ "$x" == "List" ]; 
        then continue; 
    fi;
    echo $x
    adb connect $x
done

How to only get IPs from "device" in list and skip IPs from other values in list?

CodePudding user response:

Use the "grep" command to filter by the name and no need for additional "cut"

a=$(adb devices | grep "device" | cut -f1 -d\);

CodePudding user response:

a=$(adb devices | sed "1 d" | grep "device" | cut -f1 -d\ );

Added two more filter:

sed "1 d" - omit first line, print everything else

grep "device" - print only lines that contain "device"

You can remove the if statement, as it is handled by the sed filter.

CodePudding user response:

With GNU awk. Use : and at least one space character as field separator.

adb devices | awk -F ':|  ' '$3 == "device"{ print $1 }'

Output:

192.168.1.201
192.168.1.203
  • Related