Shell operations and simple programming (5)

Source: Internet
Author: User

Standard Input and Output

When executing a shell command line, three standard documents (stdin) are automatically opened, which usually correspond to the keyboard of the terminal. stdout) and stderr correspond to the terminal screen. The process will obtain the input data from the standard input document, output the normal output data to the standard output document, and send the error information 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. The information output to the terminal screen can only be viewed. We cannot process the output more, for example, using the output as the input of another command. To solve these problems, the Linux system introduces two other mechanisms for input/output transmission, namely, input/output redirection and pipelines.

  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 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, all the text typed from the keyboard will appear on the screen, but there is no result, after pressing 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 defines a pair of delimiters (the separator is defined by the word after the <symbol, which is expressed by EOF in this example) as the standard input to the command. In the following example, the text between a pair of separator EOF is used as the input of the WC command, and the number of lines, words, and characters of the text are counted.

[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 it meets another separator. The second separator should start with a new line. At this time, the body of the here document (excluding the start and end separator) will be reoriented to the command WC as its standard 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.