【问题标题】:Linux shell: my `expect` script doesn't work as expectedLinux shell:我的 `expect` 脚本没有按预期工作
【发布时间】:2017-06-02 01:42:56
【问题描述】:

我有一个像下面这样的简单脚本,从命令行读取 2 个数字并将它们相加:

$cat runexp.sh
#!/bin/bash
echo "read 1st number"
read n1
echo "read 2nd number"
read n2
expr $n1 + $n2

它运行,没问题。然后我写了一个如下的期望脚本:

$cat autorun.sh
#!/usr/bin/expect
spawn ./runexp.sh
expect 'read 1st number' {send "1"}
expect 'read 2nd number' {send "2"}
interact

好像还是提示从命令行读取,过了好久才结束。

$./autorun.sh
spawn ./runexp.sh
read 1st number
5
4
3
5
4
3
read 2nd number
9

我哪里做错了? 谢谢。

【问题讨论】:

    标签: linux shell input expect


    【解决方案1】:

    您还必须发送换行符,否则它只会等待(至少要等到默认超时,我认为是 10 秒)。

    此外,expect 不喜欢用单引号括住字符串。像 Tcl(它来自哪里)一样,它需要双引号或大括号。这工作得很好:

    #!/usr/bin/expect
    
    spawn ./runexp.sh
    
    expect "read 1st number" {send "1\n"}
    expect {read 2nd number} {send "2\n"}
    
    interact
    

    【讨论】:

      猜你喜欢
      • 2014-05-07
      • 2019-06-16
      • 1970-01-01
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      • 2022-10-19
      • 2021-09-23
      • 2015-06-23
      相关资源
      最近更新 更多