Shell Standard Input and Output

Source: Internet
Author: User
Tags sorts
From http://blog.csdn.net/ealpha/archive/2004/10/14/136092.aspx
Standard Input and Output

When you execute a shell command, three standard documents (stdin) are automatically opened, which usually correspond to the keyboard of the terminal. standard output
Both stdout and stderr correspond to the terminal screen. The process will obtain the input data from the standard input document and output the normal output data to the standard output document.
Send the error message to the standard error document.

Take the cat command as an example. The cat command is used to read data from the file provided by the command line and send the data directly to the standard output. If you use the following command

# Cat config

The content of the config file is displayed on the screen in sequence. However, if Cat has no parameters in the command line, it reads data from the standard input and sends it to the standard output. For example:

# Cat

Hello World

Hello World

Bye

Bye

Each line entered by the user is immediately output to the screen by the cat command.

In another example, the command sort reads the document body by line (when the file name is not given in the command line, it indicates reading from the standard input), sorts it, and sends the result to the standard output. The following example reads a purchase order from the standard input and sorts it.

# Sort

Bananas

Carrots

Apples

Apples

Bananas

Carrots

At this time, we got the sorted purchase order on the screen. Directly using standard input/output documents has the following problems:

When you enter data from the terminal, you can only use the data you enter once. If you want to use the materials again next time, you have to enter them again. In addition, it is not convenient to modify input errors on the terminal. Input
The information displayed on the terminal screen cannot be viewed. We cannot process the output more, for example, using the output as the input of another command. To solve the preceding problem
The input/output redirection and pipeline are introduced in the input/output transmission.

  1. input redirection

Input redirection refers to redirecting standard input from a command or executable program to a specified file. That is to say, the input can come from a specified file instead of the keyboard. Therefore, input redirection
It is mainly used to change the input source of a command, especially those that require a large amount of input. For example, run the WC command to count the number of lines, words, and characters contained in a specified document. If you only type:

# WC

WC will wait for the user to tell it what to calculate, and shell will be like dead, from all the text typed on the keyboard will appear on the screen, but there is no result until you press Ctrl +
D. WC displays the command result on the screen. If a document name is given as a parameter of the WC command, WC will return the number of lines, words, and characters contained in the document, as shown in the following example.

# 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 WC input. The general format of input redirection is: Command <file name. You can use the following command to redirect the WC command input to the/etc/passwd file:

# WC </etc/passwd

20 23 726

Another input redirection is called the here document, which tells shell that the standard input of the current command comes from the command line. The here document uses the redirection operator <. It sets
The text between the separator signs (the separator signs are defined by the words after the <symbol, which we use EOF to represent in this example) is oriented to the command as a standard input. In the following example, a pair of separator numbers
The text between EOF is used as the WC command input to calculate the number of lines, words, and characters of the body.

[Root @ mail root] # WC <EOF

> Hello

> World

> Are you here?

> EOF

3 5 26

After the <operator, any character or word can be used as the separator before the start of the body. In this example, EOF is used as the separator. The body of the here document continues.
Until another separator is met. The second separator should start with a new line. At this time, the body of the here document (excluding the start and end separator numbers) will be reoriented to the command WC as its standard
Quasi-input.

Because most commands specify the file name of the input file on the command line in the form of parameters, input redirection is not often used. However, if you want to use a command that does not accept the file name as the input parameter and the required input content is in another document, you can use the input redirection to solve the problem.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.