如何修复 bash 脚本中的 Expect 中的“闭引号后的额外字符”错误

问题描述 投票:0回答:1

我想自动安装 Yocto 构建的 SDK。它提供了一个用于安装的 shell 脚本,但 shell 脚本需要用户输入,我希望将其自动化。我尝试将以下内容添加到应自动安装 SDK 的现有 shell 脚本中:

        ...
        echo "built SDK, install SDK"
        /usr/bin/expect -c '
                spawn /path/to/SDK/install/script.sh
                expect "Enter target directory for SDK (default: /opt/poky/3.1.5): \r"
                send -- "\r"
                expect "You are about to install the SDK to "/opt/poky/3.1.5". Proceed [Y/n]? \r"
                send -- "\r"
        '
        /bin/bash

但这就是我运行时得到的结果:

Poky (Yocto Project Reference Distro) SDK installer version 3.1.5
=================================================================
Enter target directory for SDK (default: /opt/poky/3.1.5): extra characters after close-quote
    while executing
"expect "You are about to install the SDK to "/"
$

我不确定额外的字符错误来自哪里,有人可以帮忙吗?

linux bash shell expect
1个回答
3
投票

您已将双引号放在双引号字符串中:

expect "You are about to install the SDK to "/opt/poky/3.1.5". Proceed [Y/n]? \r"
# .....^....................................^...............^...................^

你需要逃离他们

expect "You are about to install the SDK to \"/opt/poky/3.1.5\". Proceed [Y/n]? \r"
# ..........................................^................^

此外,Tcl/expect 中的

[braces]
是命令替换语法,因此一旦双引号问题得到解决,您可能会看到
invalid command name "Y/n"

正确的解决方案是使用Tcl的非插值引用机制:大括号

expect {You are about to install the SDK to "/opt/poky/3.1.5". Proceed [Y/n]? }
# .....^......................................................................^

Tcl语法规则参见http://www.tcl-lang.org/man/tcl8.6/TclCmd/Tcl.htm

© www.soinside.com 2019 - 2024. All rights reserved.