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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With