【问题标题】:I'm having problems executing the expect command in bash on OSX我在 OSX 上的 bash 中执行期望命令时遇到问题
【发布时间】:2014-09-22 13:43:43
【问题描述】:

我需要一些帮助。

操作系统:Mac osSX 10.9.4

我正在运行一个 bash 脚本,如下所示: sh create-emulator.sh AVD_16 14 /usr/local/ks-test-automation/avd-lib/x86 QVGA

代码:

create_emulator() {
    android create avd -n $Emulator -t $Target -p $Location --abi $Processor --skin $Skin --force
    expect "Android 4.4.2 is a basic Android platform." 
    expect "Do you wish to create a custom hardware profile [no]" 
    send "no\n"
}

main_method() {
    create_emulator
}

Emulator="$1"
Target="$2"
Location="$3"
Processor="$4"
Skin="$5"
main_method

使用 ctrl+c 退出脚本后的输出:

Couldn't read file "Android 4.4.2 is a basic Android platform.": no such file or directory
couldn't read file "Do you wish to create a custom hardware profile [no]": no such file or directory
/usr/local/ks-test-automation/shell-script-lib/create-emulator.sh: line 14: send: command not found

提前致谢

【问题讨论】:

  • 我很确定您的期望语法是错误的。检查expect man page
  • 我不认为expect 做你认为它做的事情。
  • @njzk2 我认为expects 的目的正是 OP 所追求的,但他没有正确使用它。
  • 我想你至少需要使用spawn 来启动android 命令。此外,您缺少作为脚本第一行的“shebang”:#!/usr/bin/expect(如果这是安装期望的位置)。鉴于您看到的错误消息,我认为您的脚本实际上被解释为普通的 shell 脚本而不是期望。
  • ^^ @zerodiff:不,你可以在 bash 下运行脚本。您可以为此运行expect -c

标签: android macos bash shell expect


【解决方案1】:

根据我的理解,您的语法肯定是错误的。扫描了expect manual page,我认为正确的语法是这样的:

create_emulator {
    expect <<END
        spawn android create avd -n $Emulator -t $Target -p $Location --abi $Processor --skin $Skin --force
        expect "Android 4.4.2 is a basic Android platform." 
        expect "Do you wish to create a custom hardware profile \[no\]" 
        send "no\r"
        expect eof
END
}

【讨论】:

  • 谢谢。但是我现在得到:spawn:找不到命令无法读取文件“Android 4.4.2 是一个基本的 Android 平台。”:没有这样的文件或目录无法读取文件“您是否希望创建自定义硬件配置文件[no]":没有这样的文件或目录 /usr/local/ks-test-automation/shell-script-lib/create-emulator.sh:第 15 行:发送:找不到命令 /usr/local/ks-test-自动化/shell-script-lib/create-emulator.sh:create-emulator.sh:第 16 行:意外标记附近的语法错误}'/usr/local/ks-test-automation/shell-script-lib/create-emulator .sh:create-emulator.sh:第 16 行:}'
  • @user3064523,我更新了期望脚本的引用。看看这对你有没有帮助。
  • @glennjackman,谢谢。但是我收到以下错误消息:expect: option requires an argument -- c usage: expect [-div] [-c cmds] [[-f] cmdfile] [args]
  • 删除“-c”并重试。让我知道是否可以解决。
  • @yodaqua 我认为expect 可以采用正则表达式,因此将括号视为正则表达式中的控制序列。在[[ 之前加一个反斜杠。我用这个改变更新了答案。
猜你喜欢
  • 1970-01-01
  • 2019-03-04
  • 2015-07-03
  • 2013-01-21
  • 1970-01-01
  • 2021-07-03
  • 1970-01-01
  • 1970-01-01
  • 2021-10-22
相关资源
最近更新 更多