Usage of expect command within a heredoc

2019-02-24 07:32发布

问题:

For the following tiny expect script for which a function was added to the bash profile:

chai() {
    expect <<- EOF
    spawn ssh myuser@myserver
    expect ': $'
    send 'mypassword\r'
    EOF
}

We get:

bash: /etc/profile: line 409: syntax error: unexpected end of file

What is wrong with that script?

回答1:

I would normally expect the heredoc terminator (EOF) to be at the start of the line e.g.

chai() {
    expect <<- EOF
    spawn ssh myuser@myserver
    expect ': $'
    send 'mypassword\r'
EOF
}

I see you're using <<- and from the linked doc:

The - option to mark a here document limit string (<<-LimitString) suppresses leading tabs (but not spaces) in the output. This may be useful in making a script more readable.

so you should check the script to see if you have a TAB preceding your commands. The EOF is subject to the same rules.

cat <<-ENDOFMESSAGE
    This is line 1 of the message.
    This is line 2 of the message.
    This is line 3 of the message.
    This is line 4 of the message.
    This is the last line of the message.
ENDOFMESSAGE
# The output of the script will be flush left.
# Leading tab in each line will not show.


标签: bash expect