-1

I want to simply test if an android device is rooted from a computer (for a root/backup/flash script). So i have to test some locations for the su binary. Here is my code :

#!/bin/sh


#####################
## ROOT CHECK

SU_LOCATIONS="/system/bin/su /system/xbin/su"

check() {
    echo "Checking for SU binary…"
    for file in $SU_LOCATIONS
    do
        suFileResult=`$ADB shell "ls $file"`
        echo "suFileResult = $suFileResult"
        echo "file         = $file"
        if [ "$suFileResult" == "$file" ];
            then echo "Su trouvé à $suFileResult"
        fi
    done
    echo "$suFileResult" > tmpbak/suFil
}

The problem is that even if file == suFileResult, "if" return false. If i remove the spaces around ==, "if" will always return TRUE…

What am i doing wrong ? If you give an other way to test the file (and where), it would be perfect.

Thanks for your answers !

PS : the string could contain spaces here, such as :

/system/bin/su: No such file or directory

EDIT After answer :

By the way, i figured how to remove this annoying \r character : add

| tr -d '\r'

will remove this character in ALL the string (not only on the end). So in my case :

suFileResult=$(adb shell "[[ -e $file ]]; echo \$?;" | tr -d '\r')
Salamandar
  • 589
  • 6
  • 16
  • Remove the semi-colon. – 323go Aug 21 '14 at 01:11
  • @323go Please, explain, why did you mark this question as duplicate of [“How to compare strings in Bash script”](http://stackoverflow.com/questions/2237080/how-to-compare-strings-in-bash-script). They have nothing in common. – Dmitry Alexandrov Aug 28 '14 at 11:40

1 Answers1

-1

By some weird reason adb returns strings with DOS line-ending (\r\n).

$ suFileResult="$(adb shell "ls $file")"
$ set | grep suFileResult
suFileResult=$'/system/bin/su\r'

So the proper condidtion statement may look like:

[[ $suFileResult == ${file}$'\r' ]]

Also, I should note that using ls for checking file existence is a quite odd approach, even though adb does not return an error code, you could print it to STDOUT:

suFileResult=$(adb shell "[[ -e $file ]]; echo \$?")
if [[ $suFileResult == $'0\r' ]]; then
    echo "Su trouvé à $file"
fi
Dmitry Alexandrov
  • 1,693
  • 12
  • 14
  • 1
    Marvelous, it works ! I think adb works like that to be compatible with Windows… But they could have changed it for the Linux version :p – Salamandar Aug 21 '14 at 01:38
  • @Salamandar Maybe. Also, I should note that using `ls` for checking file existence is a quite odd approach. See the edited answer. – Dmitry Alexandrov Aug 21 '14 at 01:51
  • Well, oddly it doesn't work : I have suFileResult to 0 even if the file doesn't exist. That's strange because when i "chroot" on the phone, i correctly have 1 or 0 depending of the existance of the file. – Salamandar Aug 21 '14 at 02:01
  • Sorry, I forgot to escape the `$?`. Fixed. – Dmitry Alexandrov Aug 21 '14 at 02:07
  • Ah yeah :) Thanks, i just began shell scripting (though i used the cli for years) and it's a pretty difficult language… ^^'' – Salamandar Aug 21 '14 at 02:10
  • @Salamandar Then as a matter for off-topic I shall turn your attention to the fact that what you’d written is not a GNU Bash script but generic POSIX shell script – yes, the hashbang – `#!/bin/sh` – it may point to Bash in your GNU distribution, but most probably it does not. You have to write `#!/bin/bash` if you want to use full feature Bash interpreter. – Dmitry Alexandrov Aug 21 '14 at 02:34
  • Yeah, for the moment I just use sh. Didn't have any problems until now, maybe sh actually points to bash when executing a script. I'll see if I need bash. – Salamandar Aug 27 '14 at 23:32