bash – 在heredoc中使用expect命令

对于以下微小的期望脚本,其函数已添加到bash配置文件中:

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

我们得到:

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

那个脚本有什么问题?

最佳答案 我通常希望
heredoc终结器(EOF)位于线路的起点,例如

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

我看到你正在使用<< – 并且来自链接的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.

所以你应该检查脚本,看看你的命令前面是否有TAB. EOF遵守相同的规则.

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