Rumah > Soal Jawab > teks badan
我的脚本是这样的
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
导出是没有问题的,但是导入的话没有效果,大神求教。。。
ringa_lee2017-04-17 16:19:38
你脚本中已经设置了变量password的值,为什么不直接用shell呢,直接执行命令mysql -uroot -p $password blog_api < 2017-01-09-12:00:09.sql
或者写成shell脚本不就可以导入了。
怪我咯2017-04-17 16:19:38
为啥非要用expect
呢,这个命令命名可以直接用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