bash 将换行符附加到 shell 脚本中的输出文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9741433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:48:12  来源:igfitidea点击:

Appending a line break to an output file in a shell script

bashshellcygwinappend

提问by Ode

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:

我有一个在 Cygwin 中执行的 shell 脚本(也许这就是问题所在)。对于这段代码,我只想写第一行,并附加一个换行符:

echo "`date` User `whoami` started the script." >> output.log
echo >> 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.

但是 output.log 文件似乎从来没有休息过。如果我多次运行脚本,就好像第二个回声没有写入文件。

I've also tried:

我也试过:

echo -e "`date` User `whoami` started the script.\n" >> output.log

It yields the same results.

它产生相同的结果。

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.

奇怪的是,如果我只是在命令行上输入上面的第二个 echo 语句,而不附加到文件中,它会为我提供带有尾随换行符的预期输出。

回答by ruakh

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:

我敢打赌,问题在于 Cygwin 正在将 Unix 行尾 (LF) 写入文件,而您正在使用需要 Windows 行尾 (CRLF) 的程序打开它。要确定是否是这种情况 - 以及一些黑客的解决方法 - 尝试:

echo "`date` User `whoami` started the script."$'\r' >> output.log

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

$'\r'末尾是一个额外的回车;它加上 Unix 行结束,将导致 Windows 行结束)。

回答by Daniel Haley

Try:

尝试:

echo "`date` User `whoami` started the script."$'\n' >> output.log

or just:

要不就:

echo $'\n' >> output.log

回答by Tanbir

Try

尝试

echo -en "`date` User `whoami` started the script.\n" >> output.log

Try issuing this multiple times. I hope you are looking for the same output.

尝试多次发布。我希望您正在寻找相同的输出。

回答by StarPinkER

You can do that without an I/O redirection:

你可以在没有 I/O 重定向的情况下做到这一点:

sed -i 's/$/\n/' filename

You can also use this command to append a newline to a list of files:

您还可以使用此命令将换行符附加到文件列表中:

find dir -name filepattern | xargs sed -i 's/$/\n/' filename

For echo, some shells implement it as a shell builtin command. It might not accept the -eoption. If you still want to use echo, try to find where the echobinary file is, using which echo. In most cases, it is located in /bin/echo, so you can use /bin/echo -e "\n"to echo a new line.

对于echo,某些 shell 将其实现为 shell 内置命令。它可能不接受该-e选项。如果您仍想使用echo,请尝试echo使用which echo. 在大多数情况下,它位于 中/bin/echo,因此您可以使用/bin/echo -e "\n"来回显新行。