简体   繁体   English

Shell脚本中的EOF问题

[英]EOF issue in shell script

This following code segment giving error: 14: syntax error: unexpected end of file 下面的代码段给出了错误: 14:语法错误:文件意外结束

#!/bin/bash
func_some()
{
    cd some_directory
    lftp -u user,'password' sftp://192.168.xx.xx <<EOF
    cd some_directory
    mget ADMS_report_*${2}${3}${4}*.txt
}
#------------------------------ Main function ------------------------------
func_some 2017 08 08 2017 08 07

But if I remove <<EOF then script just logging in but not executing subsequent command and staying logging in indefinitely. 但是,如果我删除<<EOF则脚本仅登录但不执行后续命令,并无限期保持登录状态。

What could be the solution for this? 对此有什么解决方案? What mistakes I'm making? 我正在犯什么错误?

The <<EOF indicates the start of heredoc <<EOF指示heredoc的开始

A here document is a special-purpose code block. 此处文档是专用代码块。 It uses a form of I/O redirection to feed a command list to an interactive program or a command, such as ftp, cat, or the ex text editor. 它使用I / O重定向的形式将命令列表提供给交互式程序或命令,例如ftp,cat或ex文本编辑器。

You should then close your heredoc code block, like that: 然后,您应该像这样关闭heredoc代码块:

func_some() 
{
    cd some_directory
    lftp -u user,'password' sftp://192.168.xx.xx <<EOF
    cd some_directory
    mget ADMS_report_*${2}${3}${4}*.txt
EOF
}

as otherwise it will keep on searching for the limit string ( EOF ), and fail with a syntax error, when the end of file is reached. 否则,它将继续搜索限制字符串( EOF ),并在到达文件末尾时因语法错误而失败。

Note that the limit string, must be placed at the start of the line, with no spaces in front of it. 请注意,限制字符串必须放置在行的开头,并且前面不能有空格。

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

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