bash 可以使用相同的输入文件作为管道命令的输出吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3055005/
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
Is it OK to use the same input file as output of a piped command?
提问by Amro
Consider something like:
考虑这样的事情:
cat file | command > file
Is this good practice? Could this overwrite the input file as the same time as we are reading it, or is it always read first in memory then piped to second command?
这是好的做法吗?这是否会在我们读取输入文件的同时覆盖它,或者它是否总是首先在内存中读取,然后通过管道传输到第二个命令?
Obviously, I can use temp files as intermediary step, but I'm just wondering..
显然,我可以使用临时文件作为中间步骤,但我只是想知道..
t=$(mktemp)
cat file | command > ${t} && mv ${t} file
回答by Juliano
No, it is not ok. All commands in a pipeline execute at the same time, and the shell prepares redirections before executing the commands. So, it is likely that the command will overwrite the file before cat reads it.
不行,不行。管道中的所有命令同时执行,shell 在执行命令之前准备重定向。因此,该命令很可能会在 cat 读取文件之前覆盖该文件。
You need sponge(1)from moreutils.
您需要来自 moreutils 的海绵 (1)。
回答by Dimitre Radoulov
You can also use something like this (not recommended, use explicit temp files in production code):
你也可以使用这样的东西(不推荐,在生产代码中使用显式临时文件):
{ rm file && your_command > file; } < file
回答by Dr_Hope
Not only should you NOT write your output to your input, but also you should avoid looping your output back to your input.
您不仅不应将输出写入输入,还应避免将输出循环回输入。
When dealing with big files, I tried
在处理大文件时,我试过
cat *allfastq30 > Sample_All_allfastq30
and it generated error messages:
它生成了错误消息:
cat: Sample_All_allfastq30: input file is output file

