Simple shell scripting question

pushVTEC

Senior member
Aug 30, 2003
265
0
0
I want to create a mysql database and then run some sql code from mysql.sql automating this using a shell script how would i do that, this is what i have now:

#!/bin/sh

echo Please enter hostname \(usually localhost\)
read hostname
echo Please enter your mySql Username:
read uName
echo Please enter your mySql password:
echo Desired database name:
read dbname

stty -echo
read password
stty echo

echo Running generatedb.sql...


mysql -u $uName -p $password
CREATE DATABASE $dbname;

can i just do mysql $dbname < mysql.sql after the create database line?
 

pushVTEC

Senior member
Aug 30, 2003
265
0
0
Also when I run the script and it logs in it takes me to the mysql prompt, I don't want it to do that. Just login, create the db, run the mysql.sql file, then exit the shell script if all went ok. Is it even possible to create the db via a shell script?
 

itachi

Senior member
Aug 17, 2004
390
0
0
Originally posted by: pushVTEC
can i just do mysql $dbname < mysql.sql after the create database line?
create it at the beginning of the sql file.. trying to create it outside of it makes things unnecessarily difficult.

CREATE DATABASE IF NOT EXISTS $dbname;
use $dbname;

also, the command to open mysql should be..
mysql -u $uname -p --password=$password

the user specified by $uname has to have root privileges though.

if you're trying to make it so the database is created as root only.. then u can dump the 'CREATE DATABASE..' command to a file, and redirect the stream.