简体   繁体   English

在heredoc中使用expect命令

[英]Usage of expect command within a heredoc

For the following tiny expect script for which a function was added to the bash profile: 对于以下微小的期望脚本,其函数已添加到bash配置文件中:

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? 那个脚本有什么问题?

I would normally expect the heredoc terminator (EOF) to be at the start of the line eg 我通常希望heredoc终结符(EOF)位于行的开头 ,例如

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

I see you're using <<- and from the linked doc: 我看到你正在使用<<-并且来自链接的doc:

The - option to mark a here document limit string (<<-LimitString) suppresses leading tabs (but not spaces) in the output. 标记here文档限制字符串(<< - LimitString)的 - 选项会抑制输出中的前导选项卡(但不是空格)。 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. 所以你应该检查脚本,看看你的命令前面是否有TAB The EOF is subject to the same rules. 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM