Use MySQL Commands in a Shell Script

Hello dear community.

I'm thankful to use FreeBSD v. 7.2, but I have to ask you something: can I use MySQL Commands simply with a shell script? If yes, how?

Thanks for answering,

Yacki
 
You should read the man page for the mysql command.

Have you used the mysql command before? Normally you start it from a shell, and it "logs in" to MySQL and brings you to the "MySQL prompt" where you type SQL commands and such.

If you want to execute SQL statements directly from your shell script without going into the MySQL command prompt, the mysql man page has this bit of information:

Code:
       o   --execute=statement, -e statement

           Execute the statement and quit. The default output format is like
           that produced with --batch. See Section 4.2.3.1, "Using Options on
           the Command Line", for some examples. With this option, mysql does
           not use the history file.
 
you can do something like this
Code:
echo 'update info set last_update=now();' | mysql
 
And you can process result sets. Contrived example:

~/some.sql
Code:
use database;
select field from table order by optional;

~/some.sh
Code:
for f in $(mysql --password='secret' < ~/some.sql)
do
echo $f
done

etc
 
Back
Top