简体   繁体   English

是否可以使用相同的输入文件作为管道命令的输出?

[英]Is it OK to use the same input file as output of a piped command?

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

No, it is not ok. 不,不行。 All commands in a pipeline execute at the same time, and the shell prepares redirections before executing the commands. 管道中的所有命令同时执行,并且shell在执行命令之前准备重定向。 So, it is likely that the command will overwrite the file before cat reads it. 因此,在cat读取之前,该命令很可能会覆盖该文件。

You need sponge(1) from moreutils. 你需要来自moreutils的海绵(1)

你也可以使用这样的东西(不推荐,在生产代码中使用显式临时文件):

{ rm file && your_command > file; } < file

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM