1

Im making a script where I connect to a database, look for a value and then if that value returned something then I will do something else.

ip=$("variable is defined here")

Then I connect to the MYSQL database using:

/Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot --password=*

However, this takes me to another prompt, and I need to run another command from that, I can't get it to work.

I tried this:

/Applications/MAMP/Library/bin/mysql "MYSQL argumens;";

and just:

MYSQL arguments;

None of these work and I have to pass a variable to it, what can I do now?

  • You might find the DBA SE useful. – mdpc Nov 23 '14 at 20:05
7

You can do something like this:

#! bin/bash
selectvar="SELECT * FROM test;"

mysql --user=root --password=mypass database << eof 
$selectvar
eof
3

To execute statements from the command line without an interactive prompt, use the -e option:

mysql mydb -e 'select * from foo'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.