我使用了:
cat <<<"${MSG}" > outfile
首先要写一条消息到 outfile
,然后进一步处理,从我的 awk 脚本附加到那个 outfile
。
但现在我的程序逻辑已经改变了,所以我必须先通过将行从我的 awk
程序追加到 outfile
中(从我的 bash 脚本外部调用)来填充 outfile
,然后作为最后一步,在我的 outfile
头部预先添加 ${MSG} 的 heredoc
。
如何在我的 bash 脚本中完成这项工作,而不是在 awk 脚本中?
编辑
这是 MSG 的 heredoc
:
read -r -d '' MSG << EOF
-----------------------------------------------
-- results of processing - $CLIST
-- used THRESHOLD ($THRESHOLD)
-----------------------------------------------
l
EOF
# trick to pertain newline at the end of a message
# see here: http://unix.stackexchange.com/a/20042
MSG=${MSG%l}
printf '%s\n' "$MSG" > outfile
。尽可能避免使用bashisms。 - Jensl
技巧来保留尾随换行符的情况下完成操作:IFS= read -r -d '' MSG << EOF ...
。 - mklement0IFS
值吗,还是无所谓,因为脚本已经完成了? - branquito