[Shell] basic shell function: Input/Output redirection

Source: Internet
Author: User

/* Configure /*-----------------------------------------------------------------------------------------------

@ Blackeye poet <www. chenwei. ws>

Optional -----------------------------------------------------------------------------------------------*/

Role of input/output redirection:

Output redirection saves the command execution result to a file for easy viewing.

Input redirection is to change the original keyboard input to file input.

 

First, let's take a look at the standard input and output:

[Device] [device file name] [file descriptor] [type]

Keyboard/dev/stdin 0 standard input

Display/dev/stdout 1 standard output

Display/dev/stderr2Standard Error output

 

Concepts (> Overwrite,> append)

[Correct output redirection]

Command> file # overwrite the command and output it to a file or device.

Command> file # append the command to the correct output to a file or device.

 

[Error output redirection]

Error command 2> file # overwrite the output of command errors to the specified file or device

Error command 2> file # output the command error to a specified file or device in append Mode

(Note: To save the error message, you must add 2 before the single sign or double sign, with no space in the middle)

 

[Correct output and error output are saved at the same time]

1. Command> File2> & 1# Store both the correct and error outputs to the same file in overwrite Mode

2. Command> file 2> & 1 # Save both the correct and error outputs to the same file in append mode.

3. Commands&> File # is equivalent to the first one (& the role is equivalent to 2> & 1)

4. Command &> file # is equivalent to the second

5.Command> file a 2> file B# Append the correct output to file a and append the wrong output to file 2 (useful during regular backup)

 

Ls&>/Dev/null# Save the execution result to/dev/null (devices that do not exist are about to clear meaningless results)

 

Input redirection:

[WC for keyboard input statistics]

WC [-C] file name # count the number of bytes

WC [-W] file name # count the number of words

WC [-L] file name # Number of Statistics rows

(After WC is input, other characters will be counted. CTRL + D ends)

(4 rows, 7 words, 40 characters)

 

[WC Statistical File: a unit smaller than the number]

Bytes ---------------------------------------------------------------------------------------------

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.