为什么ADB命令会中断bash脚本循环? [英] Why does ADB commands break a bash script loop?

查看:285
本文介绍了为什么ADB命令会中断bash脚本循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当从shell脚本循环中运行多个adb命令时,我注意到一个问题,大多数命令没有执行。

I'm noticing a problem when running multiple adb commands from a shell script loop most of the commands does not execute.

这是示例脚本。

脚本名称: adbscript.sh

#!/bin/bash

devicecount=0
while read device; do
    ((devicecount++))
    serialno="NA"
    appinstallcount="NA"
    echo "Processing Device #$devicecount: $device"
    # serialno=$(adb -s $device shell getprop ro.serialno)
    # appinstallcount=$(adb -s $device shell pm list packages | wc -l)
    echo -e "Device: $device | Serialno: $serialno | Apps installed: $appinstallcount\n"
done < <(adb devices | egrep "\bdevice\b" | awk '{print $1}')
echo "Finished."

带有adb命令的输出已注释掉

我已连接5台设备。从bash文件运行时,如果没有 adb 命令,则为输出。依次遍历5个循环。

I have 5 devices connected. When running from the bash file without theadb commands this is the output. It iterates through each of the 5 loops.


Processing Device #1: 192.168.15.93:5123
Device: 192.168.15.93:5123 | Serialno: NA | Apps installed: NA

Processing Device #2: 192.168.15.95:5123
Device: 192.168.15.95:5123 | Serialno: NA | Apps installed: NA

Processing Device #3: emulator-5554
Device: emulator-5554 | Serialno: NA | Apps installed: NA

Processing Device #4: 31005c77c8cfb200
Device: 31005c77c8cfb200 | Serialno: NA | Apps installed: NA

Processing Device #5: 98883837594d4f5453
Device: 98883837594d4f5453 | Serialno: NA | Apps installed: NA

Finished.

带循环的输出包含 ADB 命令

Output with loop containing ADB commands

取消对 adb shell 命令的注释时,它只能正确地迭代第一个循环的行。这是未注释 adb 命令的输出:

When the adb shell command is uncommented it only properly iterates lines of the first loop. This is the output with the adb command uncommented:


Processing Device #1: 192.168.15.93:5123
Device: 192.168.15.93:5123 | Serialno: 98883837594d4f5453 | Apps installed: 442

Finished.

有人可以解释这种现象吗?要处理所有行和所有循环,必须做什么?

Can someone explain this behavior and what would have to be done to have all the lines and all the loops processed?

顺便说一下,这是提供给脚本的输出(一个线性命令和输出):

By the way, this is the output supplied to the script (one liner command and output):

$ adb devices | egrep "\bdevice\b" | awk '{print $1}'
192.168.15.93:5123
192.168.15.95:5123
emulator-5554
31005c77c8cfb200
98883837594d4f5453


推荐答案

adb shell 将stdin连接到在设备上运行的命令,通常会消耗标准输入,直到达到EOF。因此,这些命令占用了设备的所有其余名称,导致循环退出。

adb shell connects stdin to the command running on the device, which will generally consume stdin until a EOF is reached. So these commands consume all of the rest of your device names, causing the loop to exit.

运行 adb stdin重定向,这样他们就可以立即获得EOF,而不会弄乱您要循环播放的内容:

Run adb with a stdin redirection, so they get an immediate EOF without messing with what you're trying to loop over:

serialno=$(adb </dev/null -s $device shell getprop ro.serialno)
appinstallcount=$(adb </dev/null -s $device shell pm list packages | wc -l)

这篇关于为什么ADB命令会中断bash脚本循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆