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

Time:05-31

I would like to automate the instsallation of a Yocto-built SDK. It provides a shell script for installation but the shell script requires user input, whioch I'd like to automate. I've tried adding the following to my existing shell script that should auto-install the SDK:

        ...
        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

but this is what I get when it is run:

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 "/"
$

I'm not sure where the extra character error comes from, can someone help out?

CodePudding user response:

You've put double quotes inside a double quoted string:

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

You'll need to escape them

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

Also, [braces] in Tcl/expect are the command substitution syntax, so once the double quote issue is fixed you'll probably see invalid command name "Y/n"

The proper solution is to use Tcl's non-interpolating quoting mechanism: braces

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

See http://www.tcl-lang.org/man/tcl8.6/TclCmd/Tcl.htm for the rules of Tcl syntax.

  • Related