开发者

Redirection operator in UNIX

开发者 https://www.devze.com 2023-01-28 03:16 出处:网络
Suppose I have three files file1 file2 file3 having some content Now when I do this on shell prompt cat file1 > file2 >file3

Suppose I have three files file1 file2 file3 having some content Now when I do this on shell prompt cat file1 > file2 >file3

Content of file1 is copied to file3 and file2 becomes empty

Similarly when I do cat > file1 > file2 > file3 It ask for input and this input is stored in file3 and both fi开发者_开发问答le1 and file2 are empty

and also for cat > file1 > file2 < file3 contents of file3 is copied to file2 and file1 is empty.

Can someone please explain to me what is happening I am new to UNIX. Also any website where I can learn more about these redirection operators.

Thanks


Consider how the shell processes each part of the command as it parses it:

cat file1 > file2 >file3
  1. cat file1: prepare a new process with the cat program image with argument file1. ( given 1 or more arguments, cat will read from each argument as a file and write to its output file descriptor)
  2. > file2: change the new process' output file descriptor to write to file2 instead of the current output sink (initially the console for an interactive shell) - create `file2 if necessary.
  3. > file3: change the new process' output file descriptor to write to file3 instead of the current output sink (was file2) - create file3 if necessary
  4. End of command: Spawn the new process

So in the end, file2 is created, but unused. file3 gets the data.

cat > file1 > file2 > file3
  1. cat: prepare a new process with the cat program/image with no arguments. (given no arguments, cat will read from its input file descriptor and write to its output file descriptor)
  2. > file1: change the new process' output file descriptor to write to file1 instead of the current output sink (initially the console for an interactive shell) - create file1 if necessary.
  3. > file2: change the new process' output file descriptor to write to file2 instead of the current output sink (was file1) - create file2 if necessary.
  4. > file3: change the new process' output file descriptor to write to file3 instead of the current output sink - (was file2) create file3 if necessary
  5. End of command: Spawn the new process

So in the end, file1 and file2 are created, but unused. file3 gets the data. cat waits for input on its input device (the console device as default for an interactive shell). Any input that cat receives will go to its output device (which ends up being file3 by the time the shell finished processing the command and invoked cat).

cat > file1 > file2 < file3
  1. cat: prepare a new process with the cat program/image with no arguments. (given no arguments, cat will read from its input file descriptor and write to its output file descriptor)
  2. > file1: change the new process' output file descriptor to write to file1 instead of the current output sink (initially the console for an interactive shell) - create file1 if necessary.
  3. > file2: change the new process' output file descriptor to write to file2 instead of the current output sink (was file1) - create file2 if necessary.
  4. < file3: change the new process' input file descriptor to read from file3 instead of the current input source (initially the console for an interactive shell)
  5. End of command: Spawn the new process

So in the end, file1 is created, but unused. file2 gets the data. cat waits for input on its input device (which as set to file3 by the time the shell finished processing the command and invoked cat). Any input that cat receives will go to its output device (which ends up being file2 by the time the shell finished processing the command and invoked cat).

--

Note that in the first example, cat is the one who processes/opens file1. The shell simply passed the word file1 to the program as an argument. However, the shell opened/created file2 and file3. cat knew nothing about file3 and has no idea where the stuff it was writing to its standard output was going.

In the other 2 examples, the shell opened all the files. cat knew nothing about any files. cat had no idea where its standard input was coming from and where its standard output was going to.


Per @Sorpigal comment - the BASH manual has some good descriptions of what the different redirection operators do. Much of it is the same across different Unix shells to varying degrees, but consult your specific shell manual/manpage to confirm. Thanks @Sorpigal.

http://gnu.org/software/bash/manual/html_node/Redirections.html


You can redirect the standard input < standard output 1> or > error output 2> or both outputs &> but you can only redirect 1:1, you can't redirect one output into two different files.

What you are looking for is the tee utility.


If you don't want to lose original content, you should use redirect and append >> or << operators instead. You can read more here.

0

精彩评论

暂无评论...
验证码 换一张
取 消