Home   Profile   Fun
#92 Linux  03.04.2007

Running MySQL queries from a shell script or from the command line


Execute a query from a shell script.
#!/bin/bash

password=`cat /etc/security/mysqlpassword`
name='testuser'

RESULT=`/usr/bin/mysql -u testuser -p$password -h localhost<<SQL
use database1
select * from table1 where description='test';
quit
SQL`

echo $RESULT

Another way is to put just the MySQL commands in a file and pass it to MySQL.
/usr/bin/mysql -u root -p database1 < batch.sql

To execute a query directly from the command line use -e.
/usr/bin/mysql -u root -p database1 -e "select * from table1 where description='test';"