检查adb设备连接情况

Check if adb device connected condition

如何在批处理文件中检查 adb 设备是否已连接? 我在批处理文件中写了 "adb devices",但我希望它作为一个条件,以便应用程序自动顺利运行 因此,如果用户未连接他的设备,则不打印任何设备并退出应用程序, 否则恢复应用程序。

将输出通过管道传输到 find 并分析错误级别:

adb devices -l | find "device product:" >nul
if errorlevel 1 (
    echo No connected devices
) else (
    echo Found!
    ..............
)

对于 bash 或类似 shell 环境中的用户,请尝试

$ adb get-state 1>/dev/null 2>&1 && echo 'device attached' || echo 'no device attached'

这是最简单和最准确的方法,因为它只会找到 "device" 而不会找到其他包含该字符串的单词,例如 "devices":

adb devices | findstr "\<device\>"

请注意 findstr 适用于 Windows 但不适用于 Unix 终端。所以等价的是一起使用 find 和 grep 。 John 在本页中对此进行了很好的解释 Using find and grep to Mimic findstr Command