Linux

./shell.sh: 第 6 行: [: =~: 需要二元運算符

  • July 19, 2021

我正在嘗試執行以下 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
#!/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

引用自:https://unix.stackexchange.com/questions/659038