Bash:如何避免警告:here-document在第XX行由文件结尾(想要的'EOM')分隔

Ruc*_*waj 3 bash

我在一个linux主机(bash版本3.2.25(1))上运行了一个bash脚本,因为我已经将脚本移动到另一个主机(bash verison版本4.2.25(1))它正在抛出警告如

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

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

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

}

我确保在EOM之前和之后没有空格,制表符或任何特殊符号,因为它是在谷歌研究期间发现错误的原因.

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
Run Code Online (Sandbox Code Playgroud)

这是一个脚本.sh,其中一个功能使用用法:(但我想这不是错误的原因,但可能是我错了) -

show_usage()
{

declare -i rc=0
show_error "${@}"
rc=${?}
echo "${USAGE}"
exit ${rc}  
}  
Run Code Online (Sandbox Code Playgroud)

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

cho*_*oba 9

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

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