0

This question already has an answer here:

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

marked as duplicate by Gilles 'SO- stop being evil' linux Nov 26 '18 at 21:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.