Linux Shell Series Tutorial (16) Shell input and output redirection

Source: Internet
Author: User

This article is part of the Linux Shell Series Tutorial (16), more Linux shell tutorials: Linux Shell Tutorials

The output and input redirection in the shell is a feature that is often used in the use, and is very useful for you to introduce the shell's input and output redirection knowledge today.

I. Shell input and output redirection overview

Before learning about redirection, let's take a look at Linux file descriptors.

Linux file descriptors can be understood as Linux tracking open files, and the allocation of a number, this number is similar to the C language operation of the file when the handle, through the handle can be used to implement the file read and write operations.

The user can customize the range of file descriptors: 3-max,max is related to the user's ulimit–n definition number, and cannot exceed the maximum value.

After Linux starts, 3 file descriptors are opened by default, namely:

1) Standard input input--0

2) correct output standard output--1

3) Error Output: Err output--2

For all running shell commands, there will be a default of 3 file descriptors.

When a shell command executes, there is an input: it can be entered from the keyboard, or it can be obtained from a file

After the command execution completes: successful, will output the successful result to the screen, the correct output default is the screen.

Command execution error: The error is also output to the screen, error output by default also refers to the screen.

The shell input and output redirection we introduced today is redirecting the default input and output described above to a different location.

Second, shell output redirection

Shell output redirection is mainly used as the symbol of the right angle brackets, mainly ">" and ">>" two ways.

The format is as follows:

command-line1 [1-n] > file or document operator or device command-line1 [1-n] >> file  or document operator or device

When ">" is used, the system will determine whether the right file exists, delete it first if it exists, and create a new file. Does not exist, it is created directly. Therefore, the file on the right will become empty regardless of whether the left command executes successfully.

When the ">>" operator is used, the system will determine if the right file exists and if it does not exist, create it first. Then open the file as added, and the system assigns a file descriptor bound to the standard output "1" or Error Output "2" on the left.

When the command finishes executing, the descriptor for this command binding file is also automatically invalidated. 0,1,2 will be free again.

When a command executes, the input of the command, the correct output, the error output, and the default binding of the 0,1,2 file descriptor.

A command checks that the output is correct before executing, and if the output device is wrong, no command execution is performed.

For a detailed description of the output redirection, please see the table below:

Command format Command description
Command > FileName REDIRECT standard output to a file
Command > FileName 2>&1 redirect standard output and errors together into a file
Command 2 > FileName redirect standard errors to a file
Command 2 >> filename REDIRECT standard output to a file (append)
Command >> filename2>&1 redirect standard output and errors together to a file (append)
Third, shell output redirection example

Let's look at the output redirection under the shell by example.

Example:

#date > Date.txt#cat date.txtthu June  2 15:25:20 CST 2016

This command redirects the standard output of the date command to Date.txt, and the date.txt is emptied.

Example 2:

#whoami > Date.txt#cat date.txtroot

This redirects the WhoAmI command to Date.txt, and you can see that the contents of the previous date.txt have been deleted.

Example 3:

#echo "linuxdaxue.com" >> date.txt#cat date.txtrootLinuxdaxue.com

This command outputs a string output redirected to Date.txt in append mode, and you can see that the output is appended to the end of the file.

Iv. introduction of shell input redirection

The shell input redirection is indicated mainly by the left angle brackets (less than sign) "<", and the command format is as follows:

command-line [n] <file or file descriptor & device

The command defaults to the input from the keyboard, redirected to the file from the file, or other open files as well as device input.

In this way, commands that would otherwise need to get input from the keyboard are transferred to the file read content.

Next, use the example to learn about the shell input redirection.

V. Shell Input Redirection Example

Example 1:

#cat Num.txt124357689#sort < num.txt123456789

Description: There are some numbers in the Num.txt, and then redirect the num.txt to the input of the sort command, sort sorts the numbers and then outputs them.

Vi. Shell Other redirects

As mentioned above, 2 of the Linux file descriptors represent standard errors. Now let's talk about how to redirect standard errors.

How to use:

Command > Out.put 2>&1 redirects the standard output and standard errors of command execution to Out.put (that is, the output is printed to out.put regardless of whether the command is executed correctly or incorrectly).

You can also redirect the error output to/dev/null, which means discarding this part of the output, as follows:

Command 2>/dev/null If command performs an error, redirect the wrong information to the empty device (ignoring the error output)

The above is the shell input and output redirection of the relevant introduction, we need a lot of practice, to gain proficiency.

For more Linux shell tutorials see: Linux Shell Series Tutorials

    • Copyright Notice: original article, 3 months ago, published by the University of Linux (linuxdaxue.com), a total of 2108 words.
    • reprint Please specify: Linux Shell Series Tutorial (16) Shell input and Output redirection | Linux University

Linux Shell Series Tutorial (16) Shell input and output redirection

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.