Shell script command output in if condition comparison fails to compare to same value string

9,155

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:
9,155

Related videos on Youtube

Vigintas Labakojis
Author by

Vigintas Labakojis

Updated on September 18, 2022

Comments

  • Vigintas Labakojis
    Vigintas Labakojis over 1 year

    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...

  • Vigintas Labakojis
    Vigintas Labakojis over 10 years
    Thanks but that didn't help.
  • Vigintas Labakojis
    Vigintas Labakojis over 10 years
    Superb! You're a genius. I'll remember that!
  • Tim Boland
    Tim Boland about 9 years
    god bless you :-)