I'm wondering if anyone can point me at resources dealing with the low(ish) level mechanics of how output rediction works in bash. Unfortunately, I've only been able to turn up pages and pages of the basic "> sends output to a file" guides, but nothing seems to go into more detail than that.
我想知道是否有人可以指出我处理输出结果如何在bash中工作的低(ish)级别机制的资源。不幸的是,我只能将基本“>发送输出到文件”指南的页面和页面调高,但似乎没有任何细节比这更详细。
In particular, I'm facing a strange situation whereby using the append redirector (>>) on Cygwin sometimes seems to start overwriting the target from the beginning of the file, rather than appending from the end as expected. I'm not aware of any combination of commands that can be given from bash to do this deliberately, but I wanted to get a better understanding of how the reidrection is actually handled in order to try and debug this behaviour and figure out what might be causing it.
特别是,我面临一种奇怪的情况,即在Cygwin上使用附加重定向器(>>)有时似乎开始从文件的开头覆盖目标,而不是按预期从末尾追加。我不知道可以从bash中给出的任何命令组合有意识地执行此操作,但我想更好地理解如何实际处理reidrection以尝试调试此行为并找出可能是什么造成它。
The actual output is from a Java program that outputs straightforward progress messages via System.out.println(), in case there could be a gotcha here I'm not aware of
实际输出来自一个Java程序,它通过System.out.println()输出简单的进度消息,如果可能存在问题,我不知道
1 个解决方案
#1
I don't know if this is how it works in Cygwin, but usually this kind of thing uses:
我不知道这是否在Cygwin中如何工作,但通常这种事情使用:
- fork to create a new process.
- open to open the redirection file.
- dup2 to make STDIN, STDOUT, or STDERR identical to the opened file.
- exec to run the specified command, with the streams now redirected.
fork来创建一个新进程。
打开以打开重定向文件。
dup2使STDIN,STDOUT或STDERR与打开的文件相同。
exec运行指定的命令,现在重定向流。
The difference between ">" and ">>" is usually handled by giving different flags to the open command; for ">", the file is merely opened, while for ">>" the file is opened in append mode (O_APPEND).
“>”和“>>”之间的区别通常是通过给open命令提供不同的标志来处理;对于“>”,文件仅打开,而对于“>>”,文件以追加模式打开(O_APPEND)。
I doubt that Cygwin has made any significant changes to the original BASH source code, so I suspect that what you are experiencing may be related to Cygwin's implementation of these UNIX functions on WIN32.
我怀疑Cygwin是否对原始BASH源代码做了任何重大更改,因此我怀疑您遇到的可能与Cygwin在WIN32上实现这些UNIX功能有关。
#1
I don't know if this is how it works in Cygwin, but usually this kind of thing uses:
我不知道这是否在Cygwin中如何工作,但通常这种事情使用:
- fork to create a new process.
- open to open the redirection file.
- dup2 to make STDIN, STDOUT, or STDERR identical to the opened file.
- exec to run the specified command, with the streams now redirected.
fork来创建一个新进程。
打开以打开重定向文件。
dup2使STDIN,STDOUT或STDERR与打开的文件相同。
exec运行指定的命令,现在重定向流。
The difference between ">" and ">>" is usually handled by giving different flags to the open command; for ">", the file is merely opened, while for ">>" the file is opened in append mode (O_APPEND).
“>”和“>>”之间的区别通常是通过给open命令提供不同的标志来处理;对于“>”,文件仅打开,而对于“>>”,文件以追加模式打开(O_APPEND)。
I doubt that Cygwin has made any significant changes to the original BASH source code, so I suspect that what you are experiencing may be related to Cygwin's implementation of these UNIX functions on WIN32.
我怀疑Cygwin是否对原始BASH源代码做了任何重大更改,因此我怀疑您遇到的可能与Cygwin在WIN32上实现这些UNIX功能有关。