views:

175

answers:

2

My problematic code:

testMYSQL=`mysql -u $mysqlUser -p$mysqlPass -h $mysqlHost --skip-column-names --batch -D $mysqlDB -e "SELECT $select FROM $mysqlTable WHERE nameTXT='test';"`       

$testMYSQL now contains:

test
test
test

Then I do:

TEST=$(echo $testMYSQL | wc -l)
echo "$TEST"

I would of thought that would work, but it doesn't, it returns 1

But if I put this into $testMYSQL: "test\ntest\ntest" it will say 3…

Whats going on here? does MYSQL not use new lines?

PS, I know I can use a for loop to loop though the lines then count up the lines that way, but I was hoping for a simpler solution like wc

+1  A: 

use the $() syntax whenever possible

$ testMYSQL=$(mysql -u $mysqlUser -p$mysqlPass -h $mysqlHost --skip-column-names --batch -D $mysqlDB -e "SELECT $select FROM $mysqlTable WHERE nameTXT='test';" )
$ set -- $testMYSQL
$ echo ${#}

also, if you just want a count of how many nameTXT that is "test", why not do it in mysql? something like select count($select) from $mysqlTable where nameTXT='tests';

ghostdog74
Probably because I didn't know you could :P But yeah, this was old code, but I don't see why wc -l works when it's done inside the MYSQL variable but it doesn't latter on… oh well, so long as I get the count :) Thanks!
Mint
@Mint: Because if you don't have quotes around your variable then all characters that match anything in `$IFS` are replaced with a single space.
Ignacio Vazquez-Abrams
A: 
TEST="$(echo "$testMYSQL" | wc -l)"

or

TEST="$(wc -l <<< "$testMYSQL")"
Ignacio Vazquez-Abrams