如何修复 bash 脚本中预期的 "extra characters after close-quote" 错误

how to fix "extra characters after close-quote" error from expect in bash script

我想自动安装 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 "/"
$

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

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

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 的 non-interpolating 引用机制:大括号

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