发送:spawn id exp7未打开

时间:2013-09-15 12:52:36

标签: bash unix expect

当我尝试执行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

无法找到错误的位置。

1 个答案:

答案 0 :(得分:2)

尝试正确引用变量:

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

双引号中未引用的变量受单词拆分和路径名扩展的影响。单引号下的变量不会扩展。阅读有关shell引用here的更多信息。

更新:看起来你实际上是通过here文档扩展它,但它仍然适用,因为你的参数仍然需要引用。这就是预期的输入:

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

如果你还没有引用它,它就会出现:

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

更新:

问题的原因实际上是因为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

将脚本另存为期望文件,并使用expect -f expect_file运行。