在shell脚本中将换行符附加到输出文件 [英] Appending a line break to an output file in a shell script
问题描述
我有一个在Cygwin中执行的shell脚本(也许是问题所在)。对于这段代码,我只想写第一行,并添加一个换行符:
I have a shell script that I am executing in Cygwin (maybe this is the problem). For this bit of code, I simply want to write the first line, and append a line break:
echo "`date` User `whoami` started the script." >> output.log
echo >> output.log
但是output.log文件似乎从未中断过。如果我多次运行脚本,就好像第二次回声未写入文件。
But the output.log file never seems to take the break. If I run the script multiple times, it's as if the second echo doesn't write to the file.
我也尝试过:
echo -e "`date` User `whoami` started the script.\n" >> output.log
产生相同的结果。
奇怪的是,如果我仅在命令行中输入上面的第二个echo语句,而没有追加到文件中,它会带给我预期的输出,并带有换行符。
The odd thing is if I just enter the second echo statement above on the command line, without appending to the file, it gives me the expected output with the trailing line break.
推荐答案
我敢打赌,问题在于Cygwin正在将Unix行尾(LF)写入文件,而您正在使用需要Windows行尾的程序来打开它(CRLF)。要确定是否是这种情况–以及一些骇人的解决方法—尝试:
I'm betting the problem is that Cygwin is writing Unix line endings (LF) to the file, and you're opening it with a program that expects Windows line-endings (CRLF). To determine if this is the case — and for a bit of a hackish workaround — try:
echo "`date` User `whoami` started the script."$'\r' >> output.log
(其中 $'\r'
最后是一个额外的回车符;它加上Unix行结尾将导致Windows行结尾。
(where the $'\r'
at the end is an extra carriage-return; it, plus the Unix line ending, will result in a Windows line ending).
这篇关于在shell脚本中将换行符附加到输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!