LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2011, 01:59 AM   #1
dnoob
LQ Newbie
 
Registered: Feb 2011
Posts: 17

Rep: Reputation: 0
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
 
Old 02-25-2011, 03:01 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
Old 02-26-2011, 08:42 PM   #3
dnoob
LQ Newbie
 
Registered: Feb 2011
Posts: 17

Original Poster
Rep: Reputation: 0
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
 
Old 02-27-2011, 01:13 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.

Last edited by David the H.; 02-27-2011 at 01:15 AM. Reason: addendum
 
Old 02-27-2011, 11:42 AM   #5
dnoob
LQ Newbie
 
Registered: Feb 2011
Posts: 17

Original Poster
Rep: Reputation: 0
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Searching and replacing strings in a file with strings in other files xndd Linux - Newbie 16 07-29-2010 02:40 PM
Perl eq doesn't match identical variables GDarkBladE Programming 3 10-22-2008 08:33 AM
a script to match 2 ip strings nass Slackware 2 05-30-2007 03:49 PM
Identical disks that are not identical staphanes Linux - Hardware 8 03-11-2006 11:50 AM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration