【发布时间】:2020-08-02 02:38:47
【问题描述】:
如果我在我的终端上写
cat <<-EOF
hello
EOF
我得到了预期的输出,你好。
现在,在我正在编写的脚本中,我有
PARAMS=""
while (( "$#" )); do
case "$1" in
-h|--help)
cat <<-EOF
hello
EOF
exit 0
;;
--) # end argument parsing
shift
...
但是 vscode 会突出显示 cat<<-EOF 行之后的所有内容,就好像它们都是一个字符串一样,基本上忽略了 EOF。
事实上,当我运行脚本时,我得到了一个
syntax error: unexpected end of file
错误
编辑:
如果我像这样缩进代码:
while (( "$#" )); do
case "$1" in
-h|--help)
cat <<EOF
ciao
EOF
exit 0
;;
--) # end argument parsing
shift
...
左侧有 EOF,vscode 可以正常识别它,将文件的其余部分高亮显示为普通的 bash 脚本,一切正常。但是在缩进方面这很糟糕。有没有办法用 cat 命令缩进 EOF?
【问题讨论】:
-
你能试着在 EOF 前面加一个
tab吗? -
@Philippe 是什么?
-
很确定 EOF 必须开始该行。
-
在 EOF 前面的选项卡仍然存在问题。它仅在 EOF 像@stark 所说的那样位于行首时才有效。
-
确保你已经放了一个
tab,来自文档If the redirection operator is <<-, then all leading tab characters are stripped from in- put lines and the line containing delimiter. This allows here-documents within shell scripts to be indented in a natural fashion.我刚刚测试了你的代码,它适用于tab。