linux - shell脚本中的EOF问题

标签 linux bash shell

以下代码段给出错误: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

但是如果我删除 <<EOF然后脚本只登录但不执行后续命令并无限期地保持登录状态。

解决这个问题的方法是什么?我犯了什么错误?

最佳答案

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

然后你应该关闭你的 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
}

否则它将继续搜索限制字符串 ( EOF ), 并在到达文件末尾时因语法错误而失败。

注意限制字符串,必须放在行首, 前面没有空格。

关于linux - shell脚本中的EOF问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45561437/

相关文章:

C - 创建 n 个线程

linux - 在 RedHat Linux 脚本中使用 cd

bash - 给定别名,从ssh config获取主机名

java - Autosys 文件观察器

shell - $?VAR 在 csh 中是什么意思?

linux - KVM 运行 Windows XP 的虚拟机 : How to get files from guest to host?

linux - 从 Postgresql 9.1 升级到 9.3 时为 "cannot write to log file pg_upgrade_internal.log"

mysql - 使用 Regex 解析 Bash 脚本中的 ClamAV 日志以插入 MySQL

bash - bash 命令 "rm *~"有什么作用?

c - 套接字未收到消息 AF_UNIX