Heredoc内盒有问题.Bash脚本 [英] Problem with heredoc inside case. Bash script
问题描述
如果在我的终端上写
cat <<-EOF
hello
EOF
我得到了预期的输出,你好.
I get the expected output, hello.
现在,在我正在编写的脚本中,我已经拥有
Now, In a script I'm writing I have
PARAMS=""
while (( "$#" )); do
case "$1" in
-h|--help)
cat <<-EOF
hello
EOF
exit 0
;;
--) # end argument parsing
shift
...
但是vscode突出显示了 cat<< -EOF
行之后的所有内容,就好像它们都是字符串一样,基本上忽略了EOF.实际上,当我运行脚本时,我得到了一个
But vscode is highlighting everything after the line cat<<-EOF
as if it all was a string, basically ignoring the EOF.
And in fact when I run the script I get a
syntax error: unexpected end of file
错误
如果我像这样缩进代码:
if I indent the code like this:
while (( "$#" )); do
case "$1" in
-h|--help)
cat <<EOF
ciao
EOF
exit 0
;;
--) # end argument parsing
shift
...
在左侧带有EOF的情况下,vscode可以识别它,将文件的其余部分作为普通的bash脚本显示出来,并且可以正常工作.但是从缩进角度来看,这很糟糕.有没有办法用cat命令缩进EOF?
with EOF on the left, vscode recognises it as it should, hilighting the rest of the file as a normal bash script and everithing works. But indentation-wise this sucks. Is there a way to indent EOF with the cat command?
推荐答案
-EOF必须位于该行的开头.当我漂亮地打印脚本并无意缩进Heredoc终止符时,我犯了几次这个错误.
The -EOF has to be at the beginning of the line. I've made that mistake several times when I prettyprint a script and inadvertently indent the heredoc terminator.
这篇关于Heredoc内盒有问题.Bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!