jagdpanzer
jagdpanzer

Reputation: 713

What's wrong with my string comparison?

I am having issues evaluating string expressions in my Bash script. This snippet of code finds the display size of the tablet connected and stores it in $displaySize. When echo $displaySize is called, it echos 1280x800.

displaySize=$($adb_env -s $usb shell wm size | awk '{print $3}')
echo "$displaySize"
fifteen="1920x1080"
ten="1280x800"
if [ "$displaySize" == "$fifteen" ] ; then
    echo "Configuring a 15\""
    foo ; fi
if [ "$displaySize" == "$ten" ] ; then
    echo "Configuring a 10\""
    bar ; fi

The code should skip over the $fifteen if block and run through the $ten if block, but it currently skips both. What am I doing wrong?

Upvotes: 0

Views: 54

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295403

Your variable contains a literal carriage return (which is why the cursor moves back to the beginning of the line when this character is printed). To remove it:

displaySize=${displaySize//$'\r'/}

To display it unambiguously for debugging purposes:

printf 'declareSize=%q\n' "$declareSize" >&2

Or just run set -x before running your script, which will generate logs like:

+ '[' $'1280x800\r' == 1920x1080 ']'

...making the issue obvious.

Upvotes: 5

Related Questions