我有一个脚本根据grep -c
的返回值设置值,然后我使用这些值来确定连接设备的状态,并从这些值中将设备置于特定状态。我希望脚本检查设备是否成功进入我想要的状态。它用于Android设备,所以到目前为止我已经能够逃脱fastboot wait-for-device
和adb wait-for-device
。
现在我试图让它识别恢复模式状态,我用RECC=$(adb devices | sed -n '2p' | grep -c 'recovery')
完成了这个。不幸的是,在连接恢复之前,没有命令停止脚本。我一直在玩until
,但还没有成功。
#!/bin/bash
ADBC=$(adb devices | sed -n '2p' | grep -c 'device')
FBTC=$(fastboot devices | grep -c 'fastboot')
RECC=$(adb devices | sed -n '2p' | grep -c 'recovery')
if [ $RECC = 1 ]
then
echo "******* [SUCCESS] ******"
echo "* *"
echo "* recovery connected *"
echo "* *"
echo "************************"
sleep 2
elif [ $ADBC = 1 ]
then
echo "******************************************"
echo "* *"
echo "* Not in recovery, booting to recovery *"
echo "* *"
echo "******************************************"
adb reboot recovery &> /dev/null
until [ $RECC = 1 ]
do
sleep 1
done
clear
echo "******* [SUCCESS] ******"
echo "* *"
echo "* recovery connected *"
echo "* *"
echo "************************"
sleep 2
elif [ $FBTC = 1 ]
then
echo "*********************************************"
echo "* *"
echo "* In fastboot mode, rebooting to recovery *"
echo "* *"
echo "*********************************************"
fastboot reboot recovery &> /dev/null
until [ $RECC = 1 ]
do
sleep 1
done
clear
echo "******* [SUCCESS] ******"
echo "* *"
echo "* recovery connected *"
echo "* *"
echo "************************"
sleep 2
elif [ $FBTC = 0 ] && [ $ADBC = 0 ] && [ $RECC = 0 ]
then
echo "********* [ERROR] *********"
echo "* *"
echo "* Cannot detect device! *"
echo "* *"
echo "***************************"
echo
echo
echo "====== [Press enter] ======"
read
fi
该脚本在连接adb或fastboot时启动恢复,在没有连接时抛出错误,以及检测连接时间。它挂在until
命令上。我的理解是until
命令等到值为true,然后继续脚本。恰恰相反,我到目前为止的经验告诉我,如果值为false,until
只会将自己锁定在一个循环中。
仅供参考,如果你想知道我是什么,那就是greping:
adb devices
的返回,设备处于恢复模式:
List of devices attached
0599b116 recovery
fastboot devices
的返回,设备处于快速启动模式:
0599b116 fastboot
在启用了adb的已启动设备上返回adb devices
:
List of devices attached
0599b116 device