Wednesday, March 25, 2009

Use Expect script with Database and others

Expect is the tool for automated interactive programming, and it is available for Unix/Linux, Mac, and Windows.

If you are running a command line program and want to insert, modify, or retrieve data in and out with the database, expect is the choice.

It's simple and easy to learn and use. The basic commands are expect and send. Following is a piece of code to access MySQL database with Expect script.

set passwrd something
spawn mysql -u uid -p
expect "password:"
send "$passwrd\r"
expect "mysql>"
send "use CustomerDB\r"
expect "mysql>"
send "show tables;\r"
expect "mysql>"
send "select * from Customers where City = 'New York';\r"
expect "mysql>"
send "quit\r"

However, the expect script also can be used for Oracle but it is not limited to the database application. You can use it in other interactive applications such as ftp, telnet, and software installation, and software testing, etc.

Please check Don Libes' book Exploring Expect from O'Reilly for more details.

No comments: