send: spawn id exp7 not open

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

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 credentials and connection data db_host='localhost' db_name='webui_dev' db_user='root' db_pass='' new_db_name='db_2011'  expect <<EOF   log_user 0   spawn mysql -h $db_host -u $db_user -p $db_pass 'create database $new_db_name'   expect "password:"   send "$db_pass\r"   log_user 1   expect eof EOF 

Can't find where is an error.

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!