I'm connected to my DB from the bash. I do a select count of an array and I want to stock the return in a variable. How can I do that?
I did:
var=`"select count(*) from shop_tab where catalog <> ''" | mysql -h abcdcef.com --port=3306 --user=root --password=hbbfe shop`
The request return a number but it doesn't stock into the variable.
Thanks!
EDIT: It works with this command line:
myvar = $(echo "select count(*) from shop_tab where catalog <> '';" | mysql -h abcdcef.com --port=3306 --user=root --password=hbbfe shop)
I think you are forgetting an
echo
in the pipe? Like this:An easier way is :
Moreover, I'll preconize the use of function in order to easily add options to the MySQL command without having to modifying all your script.
I find this more readable too...