Bash script - too many arguments

2019-08-06 13:28发布

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?

标签: bash shell
2条回答
相关推荐>>
2楼-- · 2019-08-06 13:34

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.

查看更多
淡お忘
3楼-- · 2019-08-06 13:57

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
查看更多
登录 后发表回答