Linux redirection (input and output redirection) Comments

We know, Linux standard input device refers to the default keyboard, standard output device refers to the default display. The input and output redirection in this section is to introduce, can be understood in the literal sense, that is:

  • Input redirection: refers to the re-designated as a new device instead of a keyboard input device;
  • Output redirection: refers to the re-designated as a new device instead of the display output device.

Typically with the results of a command file to or instead of a keyboard input device as a new, the new output device generally refers to the file

Linux input redirection

For input redirection, its effect and the need to use the symbols shown in Table 1.

Table 1 input symbols and effect redirection used in
Command symbol format effect
Command <file The specified file as a command input device
Command << delimiter It indicates reading from the standard input device (keyboard) until it encounters a delimiter stopped (not including the read data delimiter), where the delimiter character string is actually customize
Command <Document 1> File 2 The execution result is output as a command file input device 1, the command to a file 2.

[Example 1]
By default, cat command accepts standard input device (keyboard) and to the console, but if the file instead of a keyboard as an input device, then the command to the specified file as an input device, and the contents of the file is read and displayed on the console.
In / etc / passwd file (stored in the basic information for all users in the system) as an example, the following command:

[the root @ localhost ~] # CAT / etc / the passwd 
# omitted here output information, the reader can see their own 
[the root @ localhost ~] # CAT </ etc / the passwd 
# output the same command with the above

Note that, although the same results of the first row but is representative of the keyboard as an input device, and the second line is the / etc / passwd file as an input device.

[Example 2]

[root@localhost ~]# cat << 0
>c.biancheng.net
>Linux
>0
c.biancheng.net
Linux

It can be seen that, when the delimiter is specified as 0, 0 unless the input, the input data can have.

[Example 3]
First, the new text file a.tx, then execute the following commands:

[the root @ localhost ~] # CAT a.txt 
[the root @ localhost ~] # CAT </ etc / passwd> a.txt 
[the root @ localhost ~] # CAT a.txt 
# output and / etc / passwd file the same content The data

It can be seen by redirecting / etc / passwd as an input device, and output is redirected to a.txt, ultimately to copy the / etc / passwd file content into the a.txt.

Linux output redirection

Compared to the input redirection, output redirection we use higher frequencies. And, and the difference is input redirection, output redirection can also be subdivided to redirect two techniques error output and standard output redirection.
For example, using the attribute information of the ls command to view two files, respectively, but in which a file does not exist, as follows:

[root@localhost ~]# touch demo1.txt
[root@localhost ~]# ls -l demo1.txt
-rw-rw-r--. 1 root root 0 Oct 12 15:02 demo1.txt
[root@localhost ~]# ls -l demo2.txt    <-- 不存在的文件
ls: cannot access demo2.txt: No such file or directory

The above command, demo1.txt exist, so the correct number of output attribute information of the file, which is the standard output of the command execution; demo2.txt does not exist and, thus performs the error message displayed after ls command is the error output of the command.

Again, in order to output the original data on the screen in turn written to the file, the two output information will be treated differently.

On this basis, the standard output redirection and redirect the error output and each including emptying and writing additional write modes. Thus, the output is redirected, which need to use the symbols and action as shown in Table 2.

 

Symbols in Table 2 and output redirection action used
Command symbol format effect
Command> file The standard output of command to redirect output to a specified file, if the file already contains data, the existing data will be cleared, and then write new data.
Command 2> file The error output of command execution redirected to the specified file, if the file already contains data, the existing data will be cleared, and then write new data.
Command >> file The standard output of command to redirect output to a specified file, if the file already contains data, the new data is written back to the original content.
2 file command >> The error output of command execution redirected to the specified file, if the file already contains data, the new data is written back to the original content.
Command >> file 2> & 1
or
command file & >>
The standard output or error output is written to the specified file if the file already contains data, the new data is written back to the original content. Note that, in a first format, the last 2> & 1 is integral, it can be considered as fixed wording.

(Example 4) with a new "Linux" Linux.txt string text file, text file and empty demo.txt, then execute the following commands:

[root@localhost ~]# cat Linux.txt > demo.txt
[root@localhost ~]# cat demo.txt
Linux
[root @localhost ~] # CAT Linux.txt> demo.txt 
[root @ localhost ~] # CAT demo.txt 
Linux <- After Linux is here to clear the existing Linux, a new Linux written
[root@localhost ~]# cat Linux.txt >> demo.txt
[root@localhost ~]# cat demo.txt
Linux
Linux < - after additional way to write new data to the original data
[the root @localhost ~] # CAT b.txt> demo.txt 
CAT: b.txt: No File or Directory SUCH <- Error output information is output to the display still
[root @localhost ~] # CAT b.txt 2> demo.txt 
[root @ localhost ~] # CAT demo.txt 
CAT: b.txt: No SUCH File or Directory <- empty the file, and then write the error message output into the file
[root@localhost ~]# cat b.txt 2>> demo.txt
[root@localhost ~]# cat demo.txt
cat: b.txt: No such file or directory
CAT: b.txt: No SUCH File or Directory < - additional write error output

Guess you like

Origin www.cnblogs.com/yoyowin/p/12096307.html