Usage of expect command within a heredoc

泪湿孤枕 提交于 2019-12-19 21:56:08

问题


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.


来源:https://stackoverflow.com/questions/29803248/usage-of-expect-command-within-a-heredoc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!