LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to interact with mysql database using shell script (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-interact-with-mysql-database-using-shell-script-721372/)

vinaytp 04-24-2009 05:44 AM

how to interact with mysql database using shell script
 
hi all...

I am new to linux... can anyone tell me how to interact mysql database using shell script......

thanks in advance...

zhjim 04-24-2009 05:58 AM

Code:

#!/bin/bash
mysql --batch -u username -ppasswort -e "Show databases"


This will list all of the databases installed.
--batch : is just a reminder for mysql that it's called noninteractive
-u username : the username or maybe another ;)
-ppassword: the password for the user. There should be no space between password an -p

mysql also has an option to not print the name of the columns but i can't recall it. Check out man mysql

Cheers Zhjim

vinaytp 04-24-2009 06:28 AM

thanks...this is fine this executes the only one statement ..and quits..
i want to execute series of statements in a shell script like


use mysql
insert user statement;
select * from user;
etc...

sundialsvcs 04-24-2009 07:46 AM

You're using the wrong tool ... "shell scripts" really were not designed for that.

And, in Linux, you don't have to try to "make that strategy work," because you probably have a half-dozen or more good programming environments at your beck and call ... any one of which can be used to write what appears to be "a shell script," thanks to the #!commandname ("shebang") mechanism.

Perl ... PHP ... Python ... Ruby ... Scheme ... others ... "pick your poison." It is very likely that most if not all of these are already installed, and if not, they can be installed in moments at no further cost to you.

When the shell is told to execute any file, it first determines whether the file is binary or text, and, if it is text, looks in the first line for the "shebang." It then silently invokes that program to carry out the script. It makes no visible difference to the user.

Welcome to Unix/Linux ... the programming tools paradise.


All times are GMT -5. The time now is 10:35 PM.