Shell operation and simple programming (5)

xiaoxiao2021-03-06  65

Standard input and output

3 standard documents are automatically opened when performing a shell command line, namely the standard input document (stdin), usually corresponds to the keyboard of the terminal; the standard output document (stdout), and the standard error output document (stderr) corresponds to the screen. The process will get the input data from the standard input document, and output the normal output data to the standard output document, and send the error message to the standard error document.

We take a CAT command as an example. The function of the CAT command is to read the information from the files given by the command line and send them directly to the standard output. If you use the following command

# Cat config

The contents of the document config will be displayed in turn on the screen. However, if there is no parameters in the command line of the CAT, it will read the information from the standard input and send it to the standard output. E.g:

# cat

Hello World

Hello World

BYE

BYE

Each row input by the user is immediately output to the screen.

Another example, the command SORT is read into the document text by line (when the file name is given in the command line, it is sorted from standard input), and the results are sent to the standard output. The following example is to read a purchase order from the standard input and sort it.

# sort

Bananas

Carrots

Apples

Apples

Bananas

Carrots

At this time we got the sorted purchase order on the screen. There is the following questions directly using standard input / output documents:

When entering the information from the terminal, the information entered by the user can only use it once. The next time you want to re-enter when you use this information. And when entering the terminal, it is not very convenient to modify the input. The information output to the terminal screen can only be seen. We cannot do more processing for this output, such as further processing as an input to another command, and so on. In order to solve the above problem, the Linux system is input, and the output is introduced into two other mechanisms, namely the input / output redirection, and the pipe.

1. Input redirection

The input redirection refers to the standard input of the command or executable program to the specified file. That is, the input may not be from the keyboard, and from a specified file. Therefore, the input redirection is mainly used to change the input source of a command, especially the input source that requires a large number of inputs. For example, the command WC statistics specifies the number of lines, words, and characters of the document. If you type only on the command line:

# wc

WC will wait for the user to tell it what is statistics. At this time, the shell seems to die, all the text typed from the keyboard appears on the screen, but there is no result until [CTRL D], the WC will order the result Displayed on the screen. If a document name is given as a parameter of the WC command, as shown in the following example, the WC will return the number of rows, words, and characters included in the document.

# wc / etc / passwd

20 23 726 / ETC / Passwd

Another way to pass the / etc / passwd document content to the WC command is to redirect the input of the WC. The general form of the input redirection is: command

# wc

20 23 726

Another input redirector is called an Here document, telling the standard input from the current command from the command line. Here document redirect operator uses <<. It puts a pair of separate symbols (separated symbols are defined by the word after the word symbol. In this example, we use EOF to represent) as a standard input orientation to command. The following example will be the body between the pair of separated symbol EOF as the input of the WC command, statistics the number of rows, words, and the number of characters.

[root @mail root] # wc << Eof> Hello

> world

> Are you here?

> EOF

3 5 26

At the << Operator, any character or word can be used as a separate symbol before the beginning of the text, and EOF is used as the separator. The body of the Here document has continued until the other partition symbol is met. The second separator should appear at the beginning of the new line. At this time, the body of the Here document (excluding the start and end symbols) will reordforward to the command WC as its standard input.

Since most commands specify the file name of the input file on the command line in the form of parameters, the input redirection is not often used. Despite this, when a command that does not accept the file as an input parameter is used, and the input content needs to be redirected to solve the problem.

转载请注明原文地址:https://www.9cbs.com/read-106576.html

New Post(0)