LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-26-2013, 06:21 PM   #1
sanaz
Member
 
Registered: Aug 2012
Posts: 76

Rep: Reputation: Disabled
Combining two scripts


Hi all,
I want to run a few sql queries via the command line and I have the queries in a file called query.txt. I want to read query from file and execute the query.

I have a script to run sql queries in general and it works find:
Code:
#!/usr/bin/expect
spawn mysql -u root -p 
expect "*?assword:*"
send -- "vm1234\r"
send -- "\r"
expect "mysql>"
send "use homo_sapiens_core_70_37\r"
send "select avg(feature_id) from homo_sapiens_core_70_37.supporting_feature;\r"
send "quit;\r"
expect eof
and I have another script to read queries from the file:
Code:
 #!/bin/sh
fname="query.txt"
   
   exec<$fname
   while read line
   do
	c="$line";
	echo "$c";
   done

But when I combine them I will get errors, I know one of them is /bin/expect and the other one has /bin/sh and probably this is causing the error, but I do not know how to change the code.

Any help is really really appreciated.
 
Old 03-26-2013, 07:53 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 15,391

Rep: Reputation: 1652Reputation: 1652Reputation: 1652Reputation: 1652Reputation: 1652Reputation: 1652Reputation: 1652Reputation: 1652Reputation: 1652Reputation: 1652Reputation: 1652
You can run your SQL queries from the command line so using expect is not necessary. Something like:

mysql -u <user> -p<password> <database> < /path/to/database.sql
 
Old 03-27-2013, 06:43 PM   #3
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 7,806
Blog Entries: 11

Rep: Reputation: 2104Reputation: 2104Reputation: 2104Reputation: 2104Reputation: 2104Reputation: 2104Reputation: 2104Reputation: 2104Reputation: 2104Reputation: 2104Reputation: 2104
Quote:
Originally Posted by michaelk View Post
You can run your SQL queries from the command line so using expect is not necessary. Something like:

mysql -u <user> -p<password> <database> < /path/to/database.sql
I would think that is a destructive command. It will overwrite the named db.
or...
Code:
mysql -u <user> -p <userpass> -h <host> -e "select avg(feature_id) from homo_sapiens_core_70_37.supporting_feature;"
 
Old 03-27-2013, 07:25 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 15,391

Rep: Reputation: 1652Reputation: 1652Reputation: 1652Reputation: 1652Reputation: 1652Reputation: 1652Reputation: 1652Reputation: 1652Reputation: 1652Reputation: 1652Reputation: 1652
From the mysql man page:

Quote:
You can execute SQL statements in a script file (batch file) like this:

shell> mysql db_name < script.sql > output.tab
And as you posted execute a SQL query statement..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Python Scripts as a Replacement for Bash Utility Scripts LXer Syndicated Linux News 1 01-17-2013 08:08 AM
Combining scripts help investmentbnker75 Programming 3 09-26-2011 10:12 PM
help combining 2 shell scripts pbxuser911 Programming 5 10-29-2009 10:17 AM
combining scripts squall0366 Linux - Newbie 3 07-01-2009 12:58 AM
Combining 2 perl scripts together meluser Programming 5 03-18-2003 06:46 AM


All times are GMT -5. The time now is 07:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration