Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 267
Amro Avatar asked Jun 16 '10 15:06

Amro


People also ask

Can you pipe to a file?

You cannot connect a pipe to a text file, or any other file, only to processes. Second, when using a pipe the process on the left side of the pipe is the one that uses STDOUT , and the process on the right side of the pipe uses STDIN .

Which command can be used to send the output of a command to 2 different places?

"Tee" is explained in the Wikipedia article tee (command). Central is: "The tee command reads standard input, then writes its content to standard output and simultaneously copies it into the specified file(s) or variables.".

What character is used to pipe data from the output of one program to the input of another?

You can make it do so by using the pipe character '|'. Pipe is used to combine two or more commands, and in this, the output of one command acts as input to another command, and this command's output may act as input to the next command and so on.

How do you write a command output to a file?

To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.


2 Answers

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.

You need sponge(1) from moreutils.

like image 159
Juliano Avatar answered Oct 02 '22 18:10

Juliano


You can also use something like this (not recommended, use explicit temp files in production code):

{ rm file && your_command > file; } < file 
like image 44
Dimitre Radoulov Avatar answered Oct 02 '22 19:10

Dimitre Radoulov