在 shell 脚本中将换行符附加到输出文件 [英] Appending a line break to an output file in a shell script

查看:70
本文介绍了在 shell 脚本中将换行符附加到输出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 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.
" >> 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."$'
' >> output.log

(末尾的 $' ' 是一个额外的回车符;它加上 Unix 行尾,将导致 Windows 行尾).

(where the $' ' at the end is an extra carriage-return; it, plus the Unix line ending, will result in a Windows line ending).

这篇关于在 shell 脚本中将换行符附加到输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆