1

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
  • You need to quote your variable. Commented Mar 19, 2010 at 16:16

2 Answers 2

2

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';

Sign up to request clarification or add additional context in comments.

2 Comments

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: Because if you don't have quotes around your variable then all characters that match anything in $IFS are replaced with a single space.
2
TEST="$(echo "$testMYSQL" | wc -l)"

or

TEST="$(wc -l <<< "$testMYSQL")"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.