1

Why doesn't the following work? All I'm trying to do is execute a adb command and, if the response contains a certain string, then doing something about it.

I keep getting an error [; too many arguments

VARIABLE=$(adb devices);
if [ "$VARIABLE" == *list of attached* ]; then
  echo "adb command worked";
fi

Any ideas?

1
  • On another note: the command adb devices returns words List of devices attached, not list of attached. So your if statement may always return false. Commented Jan 27, 2014 at 6:10

2 Answers 2

6

Try quoting the arguments inside [[ and ]]:

VARIABLE="$(adb devices)"
if [[ "$VARIABLE" == *"list of attached"* ]]; then
  echo "adb command worked";
fi

== needs single argument on either side. When you use [ "$VARIABLE" == *list of attached* ] then *list is the first argument after == and rest are considered extra arguments.

Sign up to request clarification or add additional context in comments.

1 Comment

adb devices probably also returns a good exit code, in which case if adb devices > /dev/null 2>&1; then .. would be the correct way (but not the questioner's way) of doing this.
1

You could alternatively try using BASH's binary operator =~ to do regex matching:

VARIABLE="$(adb devices)"
if [[ $VARIABLE =~ list\ of\ attached ]]; then
    echo "adb command worked"
fi

2 Comments

Terminology hint: Wildcard characters are associated with globbing patterns - which is the feature the OP uses. The =~ operator is for regular expression-matching.
@mklement0 You are correct. Updated post to avoid any confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.