Page 1 of 1

SH file

PostPosted: Tue Jan 12, 2010 2:49 pm
by gmcust3
I am have created a SH file.

In that I have a line

mysql -uroot -pvicipass

After this Line I want to execute few sql queries.

How to do that ?

thanks

PostPosted: Tue Jan 12, 2010 3:06 pm
by brett05
ok so you want to create a sh file that will be executed after
of say same sql queries you want then and i will help you .
or create you date base file .sql then call it as this for exemple .
mysql --user="user" --password="password" < /root/sample.sql

PostPosted: Tue Jan 12, 2010 3:09 pm
by gmcust3
ok so you want to create a sh file that will be executed after
of say same sql queries you want then and i will help you .


I am looking for this.

PostPosted: Tue Jan 12, 2010 4:37 pm
by Michael_N
Enter your SQL syntax

Make the file executeble

Run it

PostPosted: Tue Jan 12, 2010 5:58 pm
by gmcust3
An Example will definitely help.

PostPosted: Tue Jan 12, 2010 6:08 pm
by Michael_N

PostPosted: Tue Jan 12, 2010 7:09 pm
by gmcust3
This is what I am trying to do ..

Sql.sh:
======================
mysql -uroot -pvicidialnow
Use asterisk;
Insert Into ...... blah blah...
Insert Into ...... blah blah...
Insert Into ...... blah blah...
Insert Into ...... blah blah...
Insert Into ...... blah blah...
Update..... ...... blah blah...
Exit

I tried chmod +x switch. But it gets stuck after getting into mysql and after I enter EXIT, then it tries to run sql command.

thanks

PostPosted: Tue Jan 12, 2010 9:36 pm
by brett05
ok we will do a small exemple here:
first thing we will create a file sample.sh
we will write this:
#!/bin/bash
echo "welcome to our script"
echo "------------------------"
echo " drop database asterisk if exist"
mysql --user=cron --password=1234 --execute="DROP DATABASE IF EXISTS `asterisk`;"
echo "-----------------------"
echo "create asterisk database"
mysql --user=cron --password=1234 --execute="create database 'asterisk' default character set utf8 collate utf8_unicode_ci;
echo "---------------------------"
echo "use database asterisk"
mysql --user=cron --password=1234 --execute="use 'asterisk';"
echo "---------------------------"
echo" "create table mytable"
mysql --user=cron --password="CREATE TABLE 'mytable' (
'P_Id' int, 'LastName' varchar(255), 'FirstName' varchar(255), 'Address 'varchar(255), 'City' varchar(255) )"
echo "---------------------------"
echo "insert data in table mytable"
mysql --user=cron --password="INSERT INTO 'mytable' ('FirstName', 'Address', 'City') VALUES ('111', '222', '333');"
echo "---------------------------"
echo "bye bye"

then save your file sample.sh for exemple in /usr/local and run it
chmod 777 -R /usr/src/sample.sh
./usr/src/sample.sh

if they are same thing mistake please correct it :wink:
enjoy

PostPosted: Tue Jan 12, 2010 9:54 pm
by gmcust3
Perfect !!!

Awesome and Thanks { though thats spoon feeding }.

Here is the Updated and a BIT corrected :

#!/bin/bash
echo "Welcome to our script"
echo "------------------------"
mysql -uroot -pvicidialnow -e "use asterisk;CREATE TABLE mytable (P_Id int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255))"
echo "---------------------------"
echo "insert data in table mytable"
mysql -uroot -pvicidialnow -e "use asterisk;INSERT INTO mytable(FirstName, Address, City) VALUES ('111', '222', '333')"
echo "---------------------------"
echo "bye bye"