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?

mklement0
  • 382,024
  • 64
  • 607
  • 775
Jaison Brooks
  • 5,816
  • 7
  • 43
  • 79
  • 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. – anishsane Jan 27 '14 at 06:10

2 Answers2

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    `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. – that other guy Jan 27 '14 at 04:00
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
John B
  • 3,566
  • 1
  • 16
  • 20
  • Terminology hint: Wildcard characters are associated with _globbing patterns_ - which is the feature the OP uses. The `=~` operator is for _regular expression_-matching. – mklement0 Jan 27 '14 at 04:13
  • @mklement0 You are correct. Updated post to avoid any confusion. – John B Jan 27 '14 at 04:23