Question

I am using expect to automate a script. Here is my script:

setup_multicraft=$(expect -c "

set timeout 10
spawn ./setup.sh

expect \"Run each Minecraft server under its own user? (Multicraft will create system users): \\\[y\\\]/n\"
send \"y\r\"

And for reference, here is some of the original script:

Run each Minecraft server under its own user? (Multicraft will create system users): [y]/n
Run Multicraft under this user: [minecraft]
User not found. Create user 'minecraft' on start of installation? [y]/n

I understand that characters like brackets, slashes, and sometimes dots need to be escaped in order for it to work properly. However I have tried several combinations of escape sequences on the characters, and I get this error each time:

invalid command name "y\"
    while executing
"y\\"
    invoked from within
"expect "Run each Minecraft server under its own user? (Multicraft will create system users): \\[y\\]/n""
spawn ./setup.sh

Thanks for reading!

Was it helpful?

Solution

The way you're declaring your expect script, in a double quoted string in the shell, is the main source of your pain. If you don't rely on interpolating shell variables, you can use a single-quoted here-doc:

setup_multicraft=$(expect <<'END_OF_EXPECT'

    set timeout 10
    spawn ./setup.sh

    expect {Run each Minecraft server under its own user? (Multicraft will create system users): [y]/n}
    send "y\r"

    # ...
END_OF_EXPECT
)

Much tidier, no?

In Tcl (and expect) command substitution is done in square brackets (equivalent to shell backticks), so if you want to use brackets in a double quoted string, they need to be escaped.

In Tcl, you can use curly braces to prevent variable expansion and command substitution (equivalent to shell single quotes). I've demonstrated that here.

The entire syntax of Tcl is described here. You would benefit from a quick run-through of the Tcl tutorial.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top