# Use MySQL Commands in a Shell Script



## Yacki (Mar 5, 2011)

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


----------



## rambetter (Mar 5, 2011)

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:


```
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.
```


----------



## Alt (Mar 5, 2011)

you can do something like this
	
	



```
echo 'update info set last_update=now();' | mysql
```


----------



## qsecofr (Mar 7, 2011)

And you can process result sets.  Contrived example:

~/some.sql

```
use database;
select field from table order by optional;
```

~/some.sh

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

etc


----------

