0

I'm trying to execute the following shell script, where I'm trying to keep executing a command in an infinite loop and until the output is not equal to a certain substring

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

but I'm getting the following error

./shell.sh: line 6: [: =~: binary operator expected
./shell.sh: line 8: [: =~: binary operator expected
Amr Kamel
  • 121
  • 3
    shellcheck.net is your friend. In this case, =~ is only recognised within [[ ... ]] brackets. – Paul_Pedant Jul 19 '21 at 14:48
  • 2
    [ and [[ are not the same thing, see e.g. Why is [ a shell builtin and [[ a shell keyword? and [What is the difference between the Bash operators [[ vs vs ( vs ((? and http://mywiki.wooledge.org/BashGuide/TestsAndConditionals – ilkkachu Jul 19 '21 at 14:51
  • 2
    also, before you trip on that, the assignment checkDeviceStatus=$(adb shell getprop sys.boot_completed 2>&1) runs the command and saves the output just once, it doesn't update every time you access the variable. So having the same condition in the if as in the surrounding while is a bit redundant, and also the loop condition will never change. You need to do the command substitution and assignment inside the loop to update it – ilkkachu Jul 19 '21 at 14:54

1 Answers1

0
#!/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
Amr Kamel
  • 121