从
shell脚本循环运行多个adb命令时,我注意到一个问题,大多数命令都没有执行.
这是一个示例脚本.
脚本名称: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 <
echo "Finished."
输出adb命令已注释掉
我连接了5台设备.当从没有theadb命令的bash文件运行时,这是输出.它遍历5个循环中的每个循环.
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命令的循环输出
取消注释adb shell命令时,它只会正确迭代第一个循环的行.这是取消注释adb命令的输出:
Processing Device #1: 192.168.15.93:5123
Device: 192.168.15.93:5123 | Serialno: 98883837594d4f5453 | Apps installed: 442
Finished.
有人可以解释这种行为,并且必须做些什么来处理所有的行和所有循环?
顺便说一句,这是提供给脚本的输出(一个衬管命令和输出):
$adb devices | egrep "\bdevice\b" | awk '{print $1}'
192.168.15.93:5123
192.168.15.95:5123
emulator-5554
31005c77c8cfb200
98883837594d4f5453