Heredocs 是很难正确处理的。如果你使用'EOF'
that's exactly结束行需要是什么,前面没有空格,就像你一样。
Alternatively, you can use the <<-
variant which strips off all leading tab characters from the lines in the heredoc and the closing line as per the following transcript (where <tab>
is the TAB character):
pax> cat <<-'eof'
...> 1
...< 2
...> <tab>eof
...> 4
...> eof
1
2
<tab>eof
4
pax> cat <<-'eof'
...> 1
...> 2
...> <tab>eof
1
2
使用<<-
变体允许更整洁的文件,但如果您想保留前导选项卡,这当然是不好的。来自bash
联机帮助页:
如果重定向运算符是<<-
,然后从输入行和包含分隔符的行中删除所有前导制表符。这允许 shell 脚本中的此处文档以自然的方式缩进。
当然,如果您只是想使用这些文件作为标志文件,有一个更好的方法cat
与定理。只需使用:
touch "$basedirectory$FEATURE_EXT.$SENT_EXT"
如果文件不存在,则创建该文件;如果存在,则更新修改时间,就像cat
但不要搞乱heredocs。不会的empty文件,但是,如果您出于某种原因需要该文件:
rm -f "$basedirectory$FEATURE_EXT.$SENT_EXT"
touch "$basedirectory$FEATURE_EXT.$SENT_EXT"
会成功的。
然而,由于定理does实际上输出一个空行(一个\n
字符),您可以选择:
echo >"$basedirectory$FEATURE_EXT.$SENT_EXT"
instead.