send: spawn id exp7 not open

后端 未结 1 780
离开以前
离开以前 2021-01-26 12:04

When I try to execute autoexpect file I get an error send: spawn id exp7 not open Here is my file sh.exp:

#!/usr/bin/expect

# mysql cr         


        
1条回答
  •  不要未来只要你来
    2021-01-26 12:36

    Try quoting your variables properly:

      spawn mysql -h "$db_host" -u "$db_user" -p "$db_pass" "create database $new_db_name"
    

    Variables not quoted in double quotes are subject to word splitting and pathname expansion. And variables under single quotes don't expand. Read more about shell quoting here.

    Update: It seems that you're actually expanding it through a here document, but it would still apply since your arguments still need to be quoted in expect. This is what it would appear as input to expect:

      log_user 0
      spawn mysql -h "localhost" -u "root" -p "" "create database db_2011"
      expect "password:"
      send "\r"
      log_user 1
      expect eof
    

    This is how it would appear if you haven't quoted it yet:

      ...
      spawn mysql -h localhost -u root -p  'create database db_2011'
      ...
    

    UPDATE:

    The cause of the problem actually is because mysql ends quickly without showing a prompt for password due to the extra argument. The solution is to send the command manually. It's also preferable to just run the whole script as an expect script, not an embedded one for lesser confusion

    #!/usr/bin/expect
    
    # mysql credentials and connection data
    set db_host "localhost"
    set db_name "webui_dev"
    set db_user "root"
    set db_pass ""
    set new_db_name "db_2011"
    
    log_user 0
    spawn mysql -h "$db_host" -u "$db_user" -p
    expect "password:"
    send "$db_pass\r"
    expect "mysql>"
    send "create database $new_db_name; exit; \r"
    log_user 1
    expect eof
    

    Save the script as an expect file and run it with expect -f expect_file.

    0 讨论(0)
提交回复
热议问题