cari

Rumah  >  Soal Jawab  >  teks badan

linux - 如何编写expect脚本自动导入mysql数据库

我的脚本是这样的
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

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

迷茫迷茫2767 hari yang lalu515

membalas semua(2)saya akan balas

  • ringa_lee

    ringa_lee2017-04-17 16:19:38

    你脚本中已经设置了变量password的值,为什么不直接用shell呢,直接执行命令mysql -uroot -p $password blog_api < 2017-01-09-12:00:09.sql或者写成shell脚本不就可以导入了。

    balas
    0
  • 怪我咯

    怪我咯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

    balas
    0
  • Batalbalas