Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm incorporating a script snippet which determines Android version via: adb shell getprop ro.build.version.release which returns: 4.1.2 or 4.0.4

The snippet currently looks like this:

if [ "$(adb shell getprop ro.build.version.release)" == "4.1.2" ]; then
    rw_path="/sdcard/"
elif [ "$(adb shell getprop ro.build.version.release)" == "4.0.4" ]; then
    rw_path="/data/local/"
fi
echo $rw_path

I thought adb might return the value via stderr just as it does with push/pull output so I tried this as well (not sure if that's the right way of doing this though):

if [ "$(adb shell getprop ro.build.version.release 2>&1)" == "4.1.2" ]; then
    rw_path="/sdcard/"
elif [ "$(adb shell getprop ro.build.version.release 2>&1)" == "4.0.4" ]; then
    rw_path="/data/local/"
fi
echo $rw_path

Tried -eq operator instead of == but it warns that "integer expression expected".

I then tried this to make sure it actually compares something:

version=$(adb shell getprop ro.build.version.release 2>&1)
if [ "$version" == "4.1.2" ]; then
    rw_path="/sdcard/"
elif [ "$version" == "4.0.4" ]; then
    rw_path="/data/local/"
fi
echo $version
echo $rw_path

Tried unquoting "$variable" to no luck. The above outputs "4.0.4" or "4.1.2" for $version as expected but in all the attempts above it fails to output anything for $rw_path.

What am I doing wrong?!

Thanks in advance.

EDIT: Tried double brackets as suggested below with no luck. How come "4.0.4" output is not equal to "4.0.4" string? Is whitespace relevant in comparison? Can I trim the output first? Just a thought...

share|improve this question
    
Whitespace is relevant. Your script works ok, the problem is with what adb returns. –  Laurentiu Roescu Sep 4 '13 at 18:55
    
Try echo "-$rw_path-" to see if returned string has some whitespaces in it. –  Laurentiu Roescu Sep 4 '13 at 18:56
    
Tried that and only the first dash was returned which confirmed that adb returns a new line at the end just as Mark Wagner said. Thanks! –  Vigintas Labakojis Sep 4 '13 at 19:05
add comment

2 Answers 2

up vote 1 down vote accepted

I suspect there is an "\r" in the output of the adb command. Try

$(adb shell getprop ro.build.version.release | tr -d '\r')

share|improve this answer
    
Superb! You're a genius. I'll remember that! –  Vigintas Labakojis Sep 4 '13 at 19:03
add comment

Try making those brackets around your test double brackets.

share|improve this answer
    
Thanks but that didn't help. –  Vigintas Labakojis Sep 4 '13 at 18:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.