linux - 如何编写expect脚本自动导入mysql数据库
迷茫
迷茫 2017-04-17 16:17:38
0
2
478

我的脚本是这样的
test.sh

#!/usr/bin/expect

set password  root
spawn mysql -u root -p

expect "password:"
send "$password\r\n"
send "drop database blog_api;\r\n"
send "CREATE DATABASE `blog_api` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;\r\n"
send "exit;\r\n"
expect "Bye"
send "mysql -uroot blog_api < 2017-01-09-12:00:09.sql;\r\n"
expect "Enter password:"
send "$password\r\n"
interact

导出是没有问题的,但是导入的话没有效果,大神求教。。。

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(2)
左手右手慢动作

The value of the variable password has been set in your script. Why not use the shell directly? Execute the command directlymysql -uroot -p $password blog_api < 2017-01-09-12:00:09.sqlor write it as a shell script and import it.

刘奇

Why do you have to use expect? This command naming can be written directly in shellexpect呢,这个命令命名可以直接用shell来写的

#!/usr/bin/env bash
mysql -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/expect

set 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 "$password\r"
expect eof

spawn mysql -uroot blog_api < 2017-01-09-12:00:09.sql;
expect "password:"
send "$password\r"
expect eof
If you must use expect, try the following🎜 rrreee
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!