LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Can not match strings that appear to be identical (https://www.linuxquestions.org/questions/programming-9/can-not-match-strings-that-appear-to-be-identical-864932/)

dnoob 02-25-2011 01:59 AM

Can not match strings that appear to be identical
 
i have an sql table with 2 columns
i run a script that randomly selects a word
from the table in column 1.
the word is displayed on the screen and I guess what it means
i concatenate the randomly selected word and the answer
the script looks for a match in mysql if it finds a match it says "Good job!" if there is no match it will say "not correct". However when i get it right it says not correct even though when i echo the variables they look exactly the same. the script below:

#!/bin/bash
var=$(mysql translator -u root --password=*-N<<EOF
SELECT word FROM tagalog ORDER BY RAND() LIMIT 1
EOF
)

echo "what does $var mean" ; read answer

var2=$(mysql translator -u root --password=*-N<<EOFMYSQL
SELECT * FROM tagalog where word like "$var" and english like "$answer"
EOFMYSQL
)
vanswer="$var $answer"

if [ "$vanswer" = "$var2" ]
then
echo "you got it right "
else
echo "you got it wrong! "
fi

echo $var2

the output from running the script with a correct answer

what does mata mean
eye
mata eye
you got it wrong!
mata eye

David the H. 02-25-2011 03:01 AM

I'm going to guess that you have non-printing or non-ascii characters of some kind in the string. Try piping all the variables through cat -A and compare the full output. e.g.:
Code:

echo "$vanswer" | cat -A

dnoob 02-26-2011 08:42 PM

thank you that was exactly the problem there was a ^I in string I could not see until echo the variable to cat -A the bash shell program is working

#!/bin/bash
loop=y
while [ "$loop" = y ] ; do

var=$(mysql translator -u root --password=presidio -N<<EOF
SELECT word FROM tagalog ORDER BY RAND() LIMIT 1
)



echo "what does $var mean" ; read answer


var2=$(mysql translator -u root --password=presidio -N<<EOFMYSQL
SELECT * FROM tagalog where word like "$var" and english like "$answer"
EOFMYSQL
)

vanswer="$var $answer" #concatenating the two string variables $var $answer

echo "$vanswer"
a=`echo "$var2" | sed -e 's/[[:cntrl:]]/ /g'| sed -e 's/\$//g'`
echo $a

if [ "$vanswer" == "$a" ]
then
echo "you got it right "
else
echo "you got it wrong! "
fi

echo "press y to continue, any other key will terminate the program"
read loop # check the value of the loop, if not "y" the program will terminate

done

David the H. 02-27-2011 01:13 AM

I thought so.

If we can determine exactly what the offending character is, we can probably use parameter substitution instead of sed. I'm pretty sure it's the tab. So try using this:
Code:

var2="${var2//$'\t'/ }"
This should replace all tabs with spaces. Or to simulate your actual sed command using the control character set:
Code:

var2="$var2//[[:cntrl:]]/ }"
There's also a tool called uniname, part of the uniutils package, that can be used to learn exactly what every character in a text string is. So if it isn't the tab, try piping it through that.

PS: In the future, please use [code][/code] tags around your code, to preserve formatting and to improve readability.

dnoob 02-27-2011 11:42 AM

it was placing these characters in between the concatenated strings ^I
i could not see it ^I until i followed your advice and piped it to cat -A
i tried some of those examples i was not able to get it to work at this time i will keep plugging
away at it thanks!


All times are GMT -5. The time now is 09:18 PM.