Input and output management in Linux

1, input and output

Stdin [standard input stream]: input refers to the system information transmitted to the external system, numbered 0

Data transmission mode are: mouse, keyboard, enter the command

stdout [standard output correctly]:

After completion of the command executed successfully generated string, stdout in Linux output number in . 1
stderr [standard error]:

Command string generated failed to complete execution, stderr in Linux output number in the range 2

2. Redirect Output

  Output redirection means, so as to cover the document, the output to the specified file or device commands.
 The correct command to redirect the output: <command> space <symbol> <file>

symbol use
     【>】           Redirect correct output
【2>】   Redirect error output
【&>】     Redirect all output

Example:

We execute the find command in a normal user, respectively, to redirect the output of this command is

find /etc/ -name passwd     #以名字的方式在etc中寻找passwd文件

We can see that execute the find command, correct output and error will be displayed on the screen

  • Correct output redirection: the correct output after the command is executed is stored in a file, the screen does not show
find /etc/ -name passwd > file  # 把正确输出保存在file文件中,屏幕只显示错误输出

  • Output redirection error: the error output after the command is executed is stored in a file, the screen does not show
find /etc/ -name passwd 2>file  #把错误输出保存在file文件中,屏幕只显示错正确输出

  • All output redirection:
find /etc/ -name passwd  &> file  #把所有输出保存在file文件中,屏幕没有输出

use:

a: execute [> filename # Clear text

b: <command> Space <symbol> / etc / null # output shield

3. Additional

<Command> Space <symbol> Space <file> output ## added to the file, the original file contents will not overwrite

      symbol use
【>>】   Additional proper output
【2>>】    Additional error output
【&>>】    Append all output

Example:

1. After the contents of file append characters (<echo> standard output corresponding to the printf)

2. After an additional content file in the content file 2

cat file2 >> file1     #追加file2的内容到file1

4. Pipeline

      <Command> + <pipeline at> + <command> # input a command into the output of the previous command (processing multiple commands duct more efficient)

symbol use

【|】       

Pipe character
【2>&1】 Converting the output of No. 1 2
【tee】

Copy output to the specified position

Example:

1. <find / etc / -name passwd 2> & 1 | wc -l> All statistical number # find command output (output of the input find wc command)

2. <find / etc / -name passwd 2> & 1 | tee file | wc -l> # put all the output of the find command file is saved in the file, but with a number of statistical wc

3. to view the history of Rule 3-5 by pipe

history | head -n 5 | tail -n 3

The head output as input history, the history displayed before row 5, then the head of the tail of the output as an input, the three lines of the display output head

5. Input redirection

Input redirection is to change the direction input, the keyboard is no longer used as a source of command input, but use the file as a command input.

command <file # file the file as a command input of
the Command << EOF  content .......  EOF     # multi-line input redirection, EOF is standard wording can also write other characters, but must be the same as before and after the start and end logo

Example:

1. uppercase file contents of the file to display all converted to uppercase

2.我们用root用户修改密码时,会输入两次密码, 因此可以用多条输入重定向来一次执行passwd命令

流标准Stdin[标准输入流输入流

发布了8 篇原创文章 · 获赞 11 · 访问量 655

Guess you like

Origin blog.csdn.net/qq_42006358/article/details/103891544