我的脚本是这样的
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
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.sql
or write it as a shell script and import it.
怪我咯2017-04-17 16:19:38
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