【问题标题】:How to continue if expected string does not appear?如果没有出现预期的字符串如何继续?
【发布时间】:2014-11-18 09:52:10
【问题描述】:

我正在使用expect 脚本来安装一些软件包。在这样的安装过程中,有时它会要求权限说Y/n 来安装它们,有时它不会。

我有两个问题:

1 ) 如何处理这两种情况?

#!/usr/bin/expect --
# This is for boto libraries installation
spawn apt-get install python-pip 
expect { 
    "Do you want to continue" 
        {   send "Y\r\n"  ## HERE SOMETIMES THIS STRING MAY NOT APPEAR
        }
}
interact
spawn pip install filechunkio 
interact
spawn pip install -U boto 
interact

当期望字符串没有出现时,它会抛出错误

spawn_id: spawn id exp6 not open
    while executing
"interact"
    (file "./botoInstall.exp" line 10)

第 10 行是第一个交互。

2 ) spawn_id: spawn id exp6 not open 是什么意思?

【问题讨论】:

  • 在没有这些混乱的情况下,将-y 传递给apt-get 是否不能满足您的要求?至少对于apt-get 步骤。
  • 对于 1) 见 Dinesh 的回答。对于 2),这是因为没有出现预期的字符串,所以 expect 命令只会在 timeouteof 上返回。由于apt-get 命令通常会很快完成(expect 命令会看到eof,这意味着生成的apt-get 进程已经退出,所以interact 命令肯定会失败。

标签: bash expect


【解决方案1】:

您必须使用exp_continueexpect 中有一个可选字符串来等待它。上面的脚本可以修改为

spawn apt-get install python-pip 

expect { 
    "Do you want to continue" {   send "Y\r\n"; exp_continue }
    #some other expect string along with 'exp_continue'
    timeout { puts "timeout happened" }
    eof { #some other action here# } 
}

如果expect 看到这些词,它将发送y\r\n,否则它将继续检查其他字符串。

请记住在 exp_continue 用法中有一些退出条件。否则,如果在时间限制内没有看到它们,显然会发生超时。

关于您对spawn id exp6 not open 的查询,请查看herehere

【讨论】:

  • 我不确定exp_continue 是这里的相关细节。我认为timeout 是。虽然你需要exp_continue 如果在同一个块中还有其他字符串要匹配。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
相关资源
最近更新 更多