我在一台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, --Destinationshow_usage()
{
declare -i rc=0
show_error "${@}"
rc=${?}
echo "${USAGE}"
exit ${rc}
} 请帮助并摆脱这个警告,以及这个EOM是如何在这里工作的?
发布于 2013-08-07 20:58:46
虽然这并没有真正回答您的问题,但您是否考虑过使用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
...
"https://stackoverflow.com/questions/18103902
复制相似问题