![./shell.sh: 第 6 行: [: =~: 需要二元运算符](https://linux22.com/image/193248/.%2Fshell.sh%3A%20%E7%AC%AC%206%20%E8%A1%8C%3A%20%5B%3A%20%3D~%3A%20%E9%9C%80%E8%A6%81%E4%BA%8C%E5%85%83%E8%BF%90%E7%AE%97%E7%AC%A6.png)
我正在尝试执行以下 shell 脚本,其中我试图在无限循环中继续执行命令,直到输出不等于某个子字符串
checkDeviceStatus=$(adb shell getprop sys.boot_completed 2>&1)
function Check_Status () {
while [ ! "$checkDeviceStatus" =~ "device offline" ] || [ ! "$checkDeviceStatus" =~ "device still authorizing" ]
do
if [ ! "$checkDeviceStatus" =~ "device offline" ] || [ ! "$checkDeviceStatus" =~ "device still authorizing" ];
then
echo "Device is now up and running!!: '$checkDeviceStatus'"
break
else
echo "'$checkDeviceStatus'"
fi;
done
};
Check_Status
但我收到以下错误
./shell.sh: line 6: [: =~: binary operator expected
./shell.sh: line 8: [: =~: binary operator expected
答案1
#!/bin/bash
function Check_Status () {
while [[ "$(adb shell getprop sys.boot_completed 2>&1)" =~ "device offline" ]] || [[ "$(adb shell getprop sys.boot_completed 2>&1)" =~ "device still authorizing" ]] || [[ "$(adb shell getprop sys.boot_completed 2>&1)" =~ "no devices/emulators found" ]];
do
sleep 1
if [[ "$(adb shell getprop sys.boot_completed 2>&1)" == "" ]] || [[ "$(adb shell getprop sys.boot_completed 2>&1)" == 1 ]];
then
echo "Device is now up and running!!: '$(adb shell getprop sys.boot_completed 2>&1)'"
break
else
echo "'$(adb shell getprop sys.boot_completed 2>&1)':("
fi
done
};
Check_Status