linux - 如何编写expect脚本自动导入mysql数据库
问题描述
我的脚本是这样的test.sh
#!/usr/bin/expectset password rootspawn mysql -u root -pexpect 'password:'send '$passwordrn'send 'drop database blog_api;rn'send 'CREATE DATABASE `blog_api` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;rn'send 'exit;rn'expect 'Bye'send 'mysql -uroot blog_api < 2017-01-09-12:00:09.sql;rn'expect 'Enter password:'send '$passwordrn'interact
导出是没有问题的,但是导入的话没有效果,大神求教。。。
问题解答
回答1:你脚本中已经设置了变量password的值,为什么不直接用shell呢,直接执行命令mysql -uroot -p $password blog_api < 2017-01-09-12:00:09.sql或者写成shell脚本不就可以导入了。
回答2:为啥非要用expect呢,这个命令命名可以直接用shell来写的
#!/usr/bin/env bashmysql -uroot -p ’root’ -e ’drop database if exists blog_api; CREATE DATABASE `blog_api` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;’mysql -uroot -p ’root’ blog_api < 2017-01-09-12:00:09.sql
如果非要用expect的话,试试下面这样
#!/usr/bin/expectset password ’root’spawn mysql -uroot -p -e 'drop database if exists blog_api; create database `blog_api` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;'expect 'password:'send '$passwordr'expect eofspawn mysql -uroot blog_api < 2017-01-09-12:00:09.sql;expect 'password:'send '$passwordr'expect eof
相关文章:
1. Java中main方法里面的参数一定要是String []args吗?2. 系统重装后,恢复mysql5.5?3. docker绑定了nginx端口 外部访问不到4. javascript - node.js中stat() access() open() readFile()都能判断文件是否存在?5. html5 - 目前 公司App 嵌入H5页面 做个 手机支付功能 没有做过 所以 请求各位有经验的 给个思路6. font-family - 我引入CSS3自定义字体没有效果?7. mysql - sql 左连接结果union右连接结果,导致重复性计算怎么解决?8. css - 如何讓圖片像雲一樣的行為?9. javascript - js 多维数组的问题10. node.js - Nodejs微信开发,视频上传成功,但是不返回给用户

网公网安备