Bash:如何避免警告:此处文档位于第 XX 行,由文件尾分隔(需要“EOM”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18103902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash: How to avoid warning: here-document at line XX delimited by end-of-file (wanted `EOM')
提问by Ruchir Bharadwaj
I was a running a bash script fine on one of the linux host(bash version version 3.2.25(1)), since I have moved the script to another host (bash verison version 4.2.25(1)) it is throwing warning as
我在其中一个 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')
The code in question is:-(not sure how EOM works)
有问题的代码是:-(不确定 EOM 是如何工作的)
USAGE=$(cat <<EOM
Usage:${BASENAME} [-h] [-n] [-q]
-h, --help This usage message
-n, --dry-run
-q, --quiet
-d, --Destination
EOM)
}
}
I have made sure there is no space, tab or any special symbols before and after EOM as it was cause of error find during research on google.
我确保在 EOM 前后没有空格、制表符或任何特殊符号,因为这是在 google 研究期间发现错误的原因。
bash (bash -x) debugged output looks like:-
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
This is sourcing a script.sh where usage is used in one of the function as: (However i guess this is not the cause of the error but may be I am wrong)-
这是采购一个 script.sh,其中在其中一个函数中使用用法为:(但是我想这不是错误的原因,但可能是我错了)-
show_usage()
{
declare -i rc=0
show_error "${@}"
rc=${?}
echo "${USAGE}"
exit ${rc}
}
Please help and getting rid of this warning and how this EOM working here exactly?
请帮助并摆脱此警告以及此 EOM 究竟是如何在这里工作的?
回答by choroba
When using a here-document, make sure the delimiter is the only string on the line. In your case, the right parenthesis is taken as part of the word, so the matching delimiter is not found. You can safely move it to the next line:
使用 here-document 时,请确保分隔符是该行中唯一的字符串。在您的情况下,右括号被视为单词的一部分,因此找不到匹配的分隔符。您可以安全地将其移动到下一行:
USAGE=$(cat <<EOM
Usage:${BASENAME} [-h] [-n] [-q]
-h, --help This usage message
-n, --dry-run
-q, --quiet
-d, --Destination
EOM
)
回答by helpermethod
While this does not really answer you question, have you considered using read
instead of cat
?
虽然这并不能真正回答您的问题,但您是否考虑过使用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"
It reads the content of the here string into the variable usage, which you can then output using echo or printf.
它将 here 字符串的内容读入变量用法,然后您可以使用 echo 或 printf 输出。
The advantage is that read
is a bash builtin and thus faster than cat
(which is an external command).
优点是它read
是内置的 bash,因此比cat
(这是一个外部命令)更快。
You can also simply embed newlines in a string:
您也可以简单地在字符串中嵌入换行符:
usage="Usage:${BASENAME} [-h] [-n] [-q]
-h, --help This usage message
...
"