[: =: unary operator expected

9,911

In you tests, if the command substitution $(adb -s ...) generates a multi-word string, the shell will treat it as a syntax error. You will have to quote the expansions:

[ "$(adb -s ...)" = *"Is the system running?"* ]

The result of an unquoted command substitution will undergo splitting and filename globbing just as an unquoted variable expansion does. Quoting the expansion avoids this. In your case, it's the splitting that causes issues.

With the command substitution expanded, the test may look something like

if [ word1 word2 word3 = *"some string"* ]

This is clearly a syntax error.

Share:
9,911

Related videos on Youtube

the_prole
Author by

the_prole

Updated on September 18, 2022

Comments

  • the_prole
    the_prole almost 2 years

    I need to write a script to test if a service inside of an Android emulator is ready for apk installation or not using this command

    adb -s emulator-5554 shell pm list package | grep package:com.android.
    

    If the command does not output any one of these substrings in the first line, then the avd is not ready for apk installation

    Is the system running?

    Can't find service

    online

    This is my script

    if [ $(adb -s emulator-5554 shell pm list package | grep package:com.android. | head -n 1) = *"Is the system running?"* ] ||
       [ $(adb -s emulator-5554 shell pm list package | grep package:com.android. | head -n 1) = *"Can't find service"* ] ||
       [ $(adb -s emulator-5554 shell pm list package | grep package:com.android. | head -n 1) = *"online"* ] ; then
      echo "could not find package service"
    else
      echo "found package service"    
    fi  
    

    but I get this error

    ./script.sh: line 4: [: =: unary operator expected
    ./script.sh: line 5: [: =: unary operator expected
    ./script.sh: line 6: [: =: unary operator expected