在 heredoc 中使用 expect 命令

Usage of expect command within a 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) 位于行的 start,例如

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

我看到您正在使用 <<- 并来自链接文档:

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.