bash 发送:spawn id exp7 未打开

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18812597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:34:12  来源:igfitidea点击:

send: spawn id exp7 not open

bashunixexpect

提问by Andrey Yasinishyn

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

当我尝试执行 autoexpect 文件时出现错误send: spawn id exp7 not open这是我的文件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.

找不到哪里出错了。

回答by konsolebox

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.

没有用双引号引用的变量会受到分词和路径名扩展的影响。单引号下的变量不会扩展。在此处阅读有关 shell 引用的更多信息。

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:

更新:似乎您实际上是通过 here 文档对其进行扩展,但它仍然适用,因为您的参数仍然需要在 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

问题的原因实际上是因为mysql由于额外的参数而很快结束而没有显示密码提示。解决方法是手动发送命令。最好将整个脚本作为预期脚本运行,而不是嵌入脚本以减少混淆

#!/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.

将脚本另存为期望文件并使用expect -f expect_file.