3

I'm trying to run a command and interpret the results, but whatever I do I get a "command not found" error. Here's a representative version of my code:

devicename="emulator-5554"
search=$(adb devices | grep -w "$devicename" | grep -w device)

until $search; do
     echo  "Waiting..."
     sleep 10
done

I've tried every variation that I can think of, including ...

search=$(adb devices | grep -w $devicename | grep -w device)

and

search=$(adb devices | grep -w ${devicename} | grep -w device)

..., but all return the same error.

How can I get the variable to be interpreted correctly?

1 Answer 1

6

The code you have runs the adb|grep|grep pipeline once only and stores the output in $search. Reading from $search doesn't re-run the pipeline.

Don't use variables to hold commands. Use functions.

search() {
    adb devices | grep -w "$devicename" | grep -qw device
}

until search; do
     echo  "Waiting..."
     sleep 10
done

Notice that I added -q to silence the final grep. You don't need to know what it found, just that it found something. Its exit code is all that matters; its output is irrelevant.

You could inline the function if you want.

until adb devices | grep -w "$devicename" | grep -qw device; do
     echo  "Waiting..."
     sleep 10
done

Or you could make $devicename a parameter, if you wish.

search() {
    adb devices | grep -w "$1" | grep -qw device
}

until search "$devicename"; do
     echo  "Waiting..."
     sleep 10
done
Sign up to request clarification or add additional context in comments.

Comments

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.