Reputation: 713
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
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