1
$ echo $(adb shell getprop service.adb.root)
1
$ while [[ $(adb shell getprop service.adb.root) != "1" ]]; do echo -n .; done
.........^C

[[ "1" != "1" ]] should be false and the while loop should not run. But, it runs forever. What's the reason and the fix?

I am trying to write a loop that will wait until the adbd daemon on the connected Android device restarts as root.

3
  • 1
    Can you show output of: echo $(adb shell getprop service.adb.root) | cat -vte Commented Feb 12, 2014 at 21:29
  • 1
    make that adb shell getprop service.adb.root | cat -vte Commented Feb 12, 2014 at 21:30
  • The correct loop is as follows. When adb is restarting on the device (as root), the adb shell command will print errors. This loop discards the error messages. while [[ $(adb shell getprop service.adb.root 2> /dev/null) != $'1\r' ]]; do echo -n .; done Commented Feb 12, 2014 at 22:08

2 Answers 2

3

The result from the getprop command contains a carriage return (ASCII 0x0d). Since you're making a string -- not a numeric -- comparison, you're effectively running:

[[ "1" != "1\r" ]]

The simplist way to fix this is to explicitly strip the carriage return:

x=$(adb shell getprop  service.adb.root | tr -d '\015')

Now your comparison should work.

You can see exactly what getprop is returning by running:

$ adb shell getprop  service.adb.root | od -c
0000000   1  \r  \n
0000003
Sign up to request clarification or add additional context in comments.

1 Comment

I am accepting this answer since it explains the problem, but I like the simpler fix provided by anubhava.
3

Since output of adb command has trailing \r you can use this while loop instead:

while [[ $(adb shell getprop service.adb.root) != $'1\r' ]]; do echo -n .; done

i.e. compare numbers not strings.

8 Comments

Why? What's the reason?
I like your fix, but accepted the other answer because it explained the problem. My low rep does not allow me to vote for your answer.
This is to make sure any string starting with "1" is matched as I wrote in my answer.
Even simpler, use (( $(adb ...) != 1 )); the arithmetic context will cause the trailing non-digits from the output of adb to be ignored.
1* is overly broad: it will return true if the abd command returns "12". Since this is bash, you can check for a CR directly: [[ $out == $'1\r' ]]
|

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.