【问题标题】:Bash: How to avoid warning: here-document at line XX delimited by end-of-file (wanted `EOM') [duplicate]Bash:如何避免警告:第 XX 行的此处文档由文件结尾分隔(需要“EOM”)[重复]
【发布时间】:2013-08-08 20:38:28
【问题描述】:

我在其中一台 linux 主机(bash 版本 3.2.25(1))上运行 bash 脚本很好,因为我已将脚本移动到另一台主机(bash 版本 4.2.25(1))它正在抛出警告

line 36: warning: here-document at line 30 delimited by end-of-file (wanted `EOM') (wanted `EOM')

有问题的代码是:-(不确定 EOM 是如何工作的)

USAGE=$(cat <<EOM
Usage:${BASENAME} [-h] [-n] [-q]
-h, --help        This usage message
-n, --dry-run     
-q, --quiet       
-d, --Destination 
EOM)

}

我已确保在 EOM 前后没有空格、制表符或任何特殊符号,因为这是在 google 研究期间发现错误的原因。

bash (bash -x) 调试后的输出如下:-

+ source /test/test1/script.sh
./test.sh: line 36: warning: here-document at line 30 delimited by end-of-file      
(wanted `EOM')
++ cat
+ USAGE='Usage:test [-h] [-n] [-q]
-h, --help        This usage message
-n, --dry-run     
-q, --quiet       
-d, --Destination

这是采购一个 script.sh ,其中使用在以下函数之一中: (但我猜这不是错误的原因,但可能是我错了)-

show_usage()
{

declare -i rc=0
show_error "${@}"
rc=${?}
echo "${USAGE}"
exit ${rc}  
}  

请帮助并摆脱这个警告以及这个 EOM 是如何在这里工作的?

【问题讨论】:

  • @tripleee 欺骗目标不是很好,因为它在问题中包含看似正确的代码,所有答案都只是猜测原因可能是什么,而在这个问题中,这里的原因-doc 失败是显而易见的。在寻找 stackoverflow.com/questions/44030479/… 的骗子时遇到了它;这个问题在这里似乎比它的欺骗目标要好得多。

标签: bash


【解决方案1】:

使用 here-document 时,请确保分隔符是该行中唯一的字符串。在您的情况下,右括号被视为单词的一部分,因此找不到匹配的分隔符。您可以安全地将其移至下一行:

USAGE=$(cat <<EOM
Usage:${BASENAME} [-h] [-n] [-q]
-h, --help        This usage message
-n, --dry-run     
-q, --quiet       
-d, --Destination 
EOM
)

【讨论】:

    【解决方案2】:

    虽然这并不能真正回答您的问题,但您是否考虑过使用 read 而不是 cat

    read -d '' usage <<- EOF
    Usage:${BASENAME} [-h] [-n] [-q]
    -h, --help        This usage message
    -n, --dry-run     
    -q, --quiet       
    -d, --Destination 
    EOF
    
    echo "$usage"
    

    它将here字符串的内容读取到变量usage中,然后您可以使用echo或printf输出。

    优点是read 是内置的bash,因此比cat(这是一个外部命令)更快。

    您也可以简单地在字符串中嵌入换行符:

    usage="Usage:${BASENAME} [-h] [-n] [-q]
    -h, --help     This usage message
    ...
    "
    

    【讨论】:

      猜你喜欢
      • 2015-01-27
      • 2019-08-13
      • 2020-12-24
      • 2018-05-09
      • 2012-09-12
      • 2016-04-04
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      相关资源
      最近更新 更多