Introduction to the Magic shell Command Line Input and Output Functions

Source: Internet
Author: User

Standard Input/Output and redirection: The Fedora Core Linux is released in Linux, and the development team of Red Hat Linux will continue to participate in the development of this release.

Standard Input and Output

We know that when executing a shell command line, three standard files are automatically opened, namely the standard input file (stdin), which usually corresponds to the keyboard of the terminal and the standard output file (stdout) and stderr, both of which correspond to the terminal screen. The process obtains the input data from the standard input file, outputs the normal output data to the standard output file, and sends the error information to the standard error file.
Take the cat command as an example. The function of the cat command is to read data from the file provided by the shell command line and send the data directly to the standard output. Use the following command:
$ Cat config displays the content of the config file on the screen in sequence. However, if cat's shell command line does not contain parameters, 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 file body by line (when the file name is not given in the shell 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
Bananas
Carrots

At this time, we got the sorted purchase order on the screen. Directly using the standard input/output file has the following problems: when the input data is input from the terminal, the user has spent half a day to input the data only once. The next time you want to use the data, you have to input it 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.

Input redirection

Input redirection refers to redirecting standard input of commands (or executable programs) 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 in a specified file. If you only type:

$ Wc

Wc will wait for the user to tell it what to calculate, and shell will appear as if it were dead. All the text typed from the keyboard will appear on the screen, but there is no result, wc did not write the command result on the screen until you press <ctrl + d>. If a file name is given as a parameter of the wc Command, as shown in the following example, wc returns the number of lines, words, and characters contained in the file.

$ Wc/etc/passwd
20 23 726/etc/passwd another way to pass the/etc/passwd file 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 the shell that the standard input of the current command comes from the shell command line. The here document uses the redirection operator <. It redirects the text between a pair of separators (represented by delim in this example) to the command. In the following example, the text between a pair of delim separators is used as the wc command input, and the number of lines, words, and characters of the text are counted.
$ Wc < > This text forms the content
> Of the here document, which
> Continues until the end
> Text delimter
> Delim

After the <operator, any character can be used as the separator before the start of the body. In this example, delim is used as the separator. The body of the here document continues until another separator is met. The second separator should start with the new line. At this time, the body of the here document (excluding the start and end delimiters) will be reoriented to the command wc as its standard input.
The usage of shell command lines is as follows.

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.