1、执行命令“yum install tcl”安装tcl工具;
2、执行命令“yum install expect -y”安装expect;
3、执行命令“ cd /home ”进入服务器根目录home;
4、执行命令“touch shell_mysql.exp”创建shell脚本交互式文件
5、执行命令“vim shell_mysql.exp”编辑shell脚本文件并输入以下内容:#!/usr/bin/expectset timeout 2#登陆数据库spawn mysql -uroot -p123456send "\r"#创建数据库并设置utf编码send "create database depart;"send "\r"send "use depart;"send "\r"send "set names utf8;"send "\r"#创建表并设置2个字段send "create table student( id int(10), name varchar(50) ); "send "\r"#插入一条学生信息send "insert into student values(1,'王熙凤');"send "\r"#退出并结束登陆send "quit"send "\r"expect eof
6、执行命令“:wq”保存退出当前文件;
7、执行命令“ chmod -R 777 shell_mysql.exp ”给脚本赋予权限;
8、执行命令“ ./shell_mysql.exp ”运行shell脚本;
9、验证环节:打开桌面客户端Navicat连接数据库能够查看到我们插入的学生信息。至此shell脚本操作mysql数据库的演示已经完成。