Reputation: 2813
I am having a really difficult time figuring out how to get the results of a mysql query into a bash shell script variable.
testDBName="some_database_name"
mysqlUser="root"
mysqlPassword="password"
mysqlCmd="/Applications/MAMP/Library/bin/mysql -u $mysqlUser -p $mysqlPassword -B -N"
cmdRes=$($mysqlCmd -e 'SHOW DATABASES LIKE "$testDBName"') #THIS IS THE TROUBLESOME LINE
if [ "$cmdRes" = "" ]; then
echo "Table does not exist"
else
echo "Table already exists"
fi
The line that is giving me the trouble is "cmdRes=...". If I just hard-code a table name into that it works fine, like this:
cmdRes=$($mysqlCmd -e 'SHOW DATABASES LIKE "some_database_name"')
But I cannot wrap my head around how/why/what is going on when I have a $variable inside that thing. Based on answers to a lot of other similar-but-different questions, I've tried putting different portions of the string into different variables to cut down on quoting, I've tried single quotes, double quotes, backslashes, double-single-double quotes, curly braces, running eval, etc. -- but nothing is working.
Thanks for any help you can provide.
Upvotes: 2
Views: 8168
Reputation: 121881
The problem is that the shell won't expand anything inside the single quotes (').
One possible solution:
cmdRes=$($mysqlCmd -e "SHOW DATABASES LIKE '$testDBName'")
Upvotes: 5