关闭

Use expect command to interact with mysql in linux system

669人阅读 评论(0) 收藏 举报
分类:

The "expect" command is not install in some linux system default , in the ubuntu system , you can simplly tap "sudo apt-get install expect" to install this tool.

And then , if you want to use to in the shell script , the first line maybe like this:

#!/use/bin/expect

It just like bash or sh . check the example below:

$:~/test$ expect
expect1.1> spawn mysql -h localhost -u db_runner -p
spawn mysql -h localhost -u db_runner -p
27022
expect1.2> expect "password:"
Enter password: expect1.3> send "mysqlpassword\r"
expect1.4> expect "mysql>"

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 88
Server version: 5.5.34-0ubuntu0.13.04.1 (Ubuntu)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> expect1.5> send "show databases;\r"
expect1.6> expect "mysql>"
show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
9 rows in set (0.00 sec)

mysql> expect1.7> send "exit;\r"
expect1.8> expect "mysql>"
exit;
Bye

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:47681次
    • 积分:1189
    • 等级:
    • 排名:千里之外
    • 原创:72篇
    • 转载:19篇
    • 译文:0篇
    • 评论:2条
    文章分类