Skip to content

Commit b0768c1

Browse files
committed
优化判断用户输入选项的代码写法
1 parent e561b2b commit b0768c1

23 files changed

+61
-61
lines changed

shell/device-tools/DisplayLogcat.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ displayLogcatForDevice() {
7474
read -r cleanConfirm
7575
if [[ -z "${cleanConfirm}" ]]; then
7676
break
77-
elif [[ "${cleanConfirm}" == "y" || "${cleanConfirm}" == "Y" ]]; then
77+
elif [[ "${cleanConfirm}" =~ ^[yY]$ ]]; then
7878
adb -s "${deviceId}" logcat -c < /dev/null
7979
break
80-
elif [[ "${cleanConfirm}" == "n" || "${cleanConfirm}" == "N" ]]; then
80+
elif [[ "${cleanConfirm}" =~ ^[nN]$ ]]; then
8181
break
8282
else
8383
echo "👻 输入不正确,请输入正确的选项(y/n)"

shell/device-tools/ExportApkFile.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ waitUserInputParameter() {
2424
if [[ -z "${targetPackageName}" ]]; then
2525
echo "是否导出系统应用?(y/n):"
2626
read -r includeSystemConfirm
27-
if [[ "${includeSystemConfirm}" == "y" || "${includeSystemConfirm}" == "Y" ]]; then
27+
if [[ "${includeSystemConfirm}" =~ ^[yY]$ ]]; then
2828
includeSystemApps="true"
29-
elif [[ "${includeSystemConfirm}" == "n" || "${includeSystemConfirm}" == "N" ]]; then
29+
elif [[ "${includeSystemConfirm}" =~ ^[nN]$ ]]; then
3030
includeSystemApps="false"
3131
else
3232
echo "❌ 无效选择,已取消操作"

shell/device-tools/ManageFile.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ removeTarget() {
646646
local confirmTip="👻 确认删除【${targetType}${targetName} 吗?(y/n):"
647647
echo -e "${colorYellow}${confirmTip}${colorReset}"
648648
read -r confirmInput
649-
if [[ "${confirmInput}" != "y" && "${confirmInput}" != "Y" ]]; then
649+
if [[ ! "${confirmInput}" =~ ^[yY]$ ]]; then
650650
echo -e "\n${colorYellow}💡 已取消删除命令:${targetName}${colorReset}"
651651
return 1
652652
fi
@@ -947,7 +947,7 @@ pasteTarget() {
947947
echo -e "${colorYellow}👻 目标位置已存在同名文件/文件夹:${sourceName}${colorReset}"
948948
echo -e "是否覆盖/合并?(y/n):"
949949
read -r confirmInput
950-
if [[ "${confirmInput}" != "y" && "${confirmInput}" != "Y" ]]; then
950+
if [[ ! "${confirmInput}" =~ ^[yY]$ ]]; then
951951
echo -e "${colorYellow}💡 已取消操作${colorReset}"
952952
return 1
953953
fi

shell/device-tools/UninstallApp.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ uninstallSingleApp() {
5656
echo "💡 [${deviceId}] 设备未安装 ${packageName} 应用,跳过卸载"
5757
return 2
5858
fi
59-
if [[ ${retainDataChoice} == "y" || ${retainDataChoice} == "Y" ]]; then
59+
if [[ ${retainDataChoice} =~ ^[yY]$ ]]; then
6060
local outputPrint
6161
outputPrint=$(adb -s "${deviceId}" shell cmd package uninstall -k "${packageName}" < /dev/null 2>&1)
6262
local exitCode=$?

shell/device-tools/env/KillAdb.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ killAdbProcess() {
1919
echo "确定要杀死 adb 进程? (y/n)"
2020
while true; do
2121
read -r killConfirm
22-
if [[ "${killConfirm}" == "y" || "${killConfirm}" == "Y" ]]; then
22+
if [[ "${killConfirm}" =~ ^[yY]$ ]]; then
2323
break
24-
elif [[ "${killConfirm}" == "n" || "${killConfirm}" == "N" ]]; then
24+
elif [[ "${killConfirm}" =~ ^[nN]$ ]]; then
2525
echo "✅ 用户手动取消操作"
2626
return 0
2727
else

shell/device-tools/env/RestartAdb.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ restartAdbProcess() {
1919
echo "确定要重启 adb 进程? (y/n)"
2020
while true; do
2121
read -r restartConfirm
22-
if [[ "${restartConfirm}" == "y" || "${restartConfirm}" == "Y" ]]; then
22+
if [[ "${restartConfirm}" =~ ^[yY]$ ]]; then
2323
break
24-
elif [[ "${restartConfirm}" == "n" || "${restartConfirm}" == "N" ]]; then
24+
elif [[ "${restartConfirm}" =~ ^[nN]$ ]]; then
2525
echo "✅ 用户手动取消操作"
2626
return 0
2727
else

shell/device-tools/flash/FlashRecovery.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ flashRecoveryForDevice() {
4646
echo "这是一个危险操作,你确定要给设备刷入新的 recovery ?(y/n)"
4747
read -r flashConfirm
4848

49-
if [[ "${flashConfirm}" == "n" || "${flashConfirm}" == "N" ]]; then
49+
if [[ "${flashConfirm}" =~ ^[nN]$ ]]; then
5050
echo "✅ 用户手动取消操作"
5151
return 0
52-
elif [[ "${flashConfirm}" != "y" && "${flashConfirm}" != "Y" ]]; then
52+
elif [[ ! "${flashConfirm}" =~ ^[yY]$ ]]; then
5353
echo "❌ 无效选择,已取消操作"
5454
return 1
5555
fi

shell/device-tools/flash/FlashTempRecovery.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ flashTempRecoveryForDevice() {
9494

9595
echo "这是一个危险操作,你确定要给设备加载临时的 recovery ?(y/n)"
9696
read -r loadConfirm
97-
if [[ "${loadConfirm}" == "n" || "${loadConfirm}" == "N" ]]; then
97+
if [[ "${loadConfirm}" =~ ^[nN]$ ]]; then
9898
echo "✅ 用户手动取消操作"
9999
return 0
100-
elif [[ "${loadConfirm}" != "y" && "${loadConfirm}" != "Y" ]]; then
100+
elif [[ ! "${loadConfirm}" =~ ^[yY]$ ]]; then
101101
echo "❌ 无效选择,已取消操作"
102102
return 1
103103
fi

shell/device-tools/flash/RestartToFastboot.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ rebootToFastbootForDevices() {
3333
deviceId="$(inputMultipleAdbDevice)"
3434
echo "你确定要将设备重启到 fastboot 模式?(y/n)"
3535
read -r rebootFastbootConfirm
36-
if [[ ${rebootFastbootConfirm} == "y" || ${rebootFastbootConfirm} == "Y" ]]; then
36+
if [[ ${rebootFastbootConfirm} =~ ^[yY]$ ]]; then
3737
if [[ -n "${deviceId}" ]]; then
3838
rebootToFastboot "${deviceId}"
3939
else
@@ -43,7 +43,7 @@ rebootToFastbootForDevices() {
4343
done < <(echo "${adbDeviceIdsString}" | tr -d '\r' | grep -v '^$')
4444
fi
4545
exit 0
46-
elif [[ ${rebootFastbootConfirm} == "n" || ${rebootFastbootConfirm} == "N" ]]; then
46+
elif [[ ${rebootFastbootConfirm} =~ ^[nN]$ ]]; then
4747
echo "✅ 已取消关机操作"
4848
exit 0
4949
else

shell/device-tools/flash/RestartToRecovery.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ rebootToRecoveryForDevice() {
5050
deviceId="$(inputMultipleDevice)"
5151
echo "你确定要将设备重启到 recovery 模式?(y/n)"
5252
read -r rebootRecoveryConfirm
53-
if [[ ${rebootRecoveryConfirm} == "y" || ${rebootRecoveryConfirm} == "Y" ]]; then
53+
if [[ ${rebootRecoveryConfirm} =~ ^[yY]$ ]]; then
5454
if [[ -n "${deviceId}" ]]; then
5555
adbDeviceIdsString=$(getAdbDeviceIdsString)
5656
fastbootDeviceIdsString=$(getFastbootDeviceIdsString)
@@ -69,7 +69,7 @@ rebootToRecoveryForDevice() {
6969
rebootToRecoveryByFastboot "${fastbootDeviceId}"
7070
done < <(echo "${fastbootDeviceIdsString}" | tr -d '\r' | grep -v '^$')
7171
fi
72-
elif [[ ${rebootRecoveryConfirm} == "n" || ${rebootRecoveryConfirm} == "N" ]]; then
72+
elif [[ ${rebootRecoveryConfirm} =~ ^[nN]$ ]]; then
7373
echo "✅ 已取消操作"
7474
else
7575
echo "❌ 输入错误,取消操作"

0 commit comments

Comments
 (0)