Redirection and piping

Source: Internet
Author: User

I. Interpreting redirects

The program must be able to read input and then be processed to display the results, the program to obtain a variety of data, the program produced a variety of results. In other words, the program must have the output to read.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/85/55/wKioL1egQ6OwiLJpAAA4Vphh9aA605.png "style=" float: none; "title=" 1.png "alt=" Wkiol1egq6owiljpaaa4vphh9aa605.png "/>

Read-in data: Input

Outputs data: Output

everything in Linux is connected to files. This means that any device has a corresponding file in the system. So we can access the device through a file. For users, access to files is through the file name, the user sees the file name, but, for the kernel, all is a number, The number is the file descriptor to access the file. When you open an existing file or a new file, the kernel returns an FD. Both read and write files require the use of FD to write and read files. fd:file Descriptor (file descriptor)

Linux provides three I/O devices to Programs

Standard input (STDIN)-0 accepts input from the keyboard by default

Standard output (STDOUT)-1 Default output to terminal window

Standard error (STDERR)-2 default output to terminal window

I/O redirection: Changing the default location

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/85/55/wKiom1egQ6STCyxFAAAk_za36v8412.png "title=" 2.png " Style= "Float:none;" alt= "Wkiom1egq6stcyxfaaak_za36v8412.png"/>

when we perform the redirect we see that the results of the LS display are output to a new file, and if the file is not in TestDir, the file is created automatically.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/85/55/wKioL1egQ6TzlKcoAAAr8DzLGzk872.png "title=" 3.png " Style= "Float:none;" alt= "Wkiol1egq6tzlkcoaaar8dzlgzk872.png"/>

and I redirect ll results to the file, then we will find that the previous results are not, are overwritten, so the file is created by.

1: If the file does not exist, the system will automatically create the file.

2: When this file is present, the system will empty the contents of the file, so that the data will be written later.

3: If the output of > to an existing file, the file will be overwritten.

if we don't want to overwrite the file use >> (accumulate) on it.

Second, redirect specific usage

stdout and Tderr can be redirected to a file:

Command Action Symbol file name

The supported operating symbols are:

> redirect stdout to a file

2> redirect stderr to file

&> Redirect all output to file

> file content will be overwritten

#set-C: Prohibit overwriting of existing files, but may append mandatory Lid: >|

#set +c: Allow overlay

>> additional content based on the original content

redirect output and errors to a file

2>: Overwrite redirect error output data stream;

2>>: Append redirect error output data stream;

The standard output and error outputs are directed to different locations:

COMMAND >/path/to/file.out >/path/to/error.out

Merge standard output and error output redirect for the same data stream

&>: Overwrite redirect

&>>: Append redirect

COMMAND >/path/to/file.out 2>&1 (Order important)

COMMAND >>/path/to/file.out 2>>&1 (Order important)

Find/etc-name passwd 2>/dev/null

/dev/null Garbage bin black hole usage

If we know there will be a lot of error messages and don't want it to show up, this time use a black hole device/dev/null. It can eat all the information that guides it.

(): Merging stdout of multiple programs

(Cal 2007;cal2008) >all.txt

Standard input:< and <<

< is to say that the data that needs to be entered by the keyboard is replaced by the file contents. As shown in.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/85/55/wKioL1egQ6TQv5oCAABDUbtjDB0855.png "title=" 4.png " Style= "Float:none;" alt= "Wkiol1egq6tqv5ocaabdubtjdb0855.png"/>

< is very helpful, especially if you are sending mail in this command. So what does << mean, it represents the end of the input meaning. As

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/85/55/wKiom1egQ6WTQygMAAAc9ZyI3l8797.png "title=" 5.png " Style= "Float:none;" alt= "Wkiom1egq6wtqygmaaac9zyi3l8797.png"/>

You can see that end is the ending input and he doesn't appear in the file.

Use < to redirect standard input

Some commands can accept stdin imported from a file:

$tr ' A-Z ' A-Z ' </etc/issue

Converts a lowercase letter in a file to uppercase.

Tr-d abc</etc/issue

Remove any letters from all ABC in the issue file.

#cat > Filea

Press Ctrl+d to leave, you can use the file instead of the keyboard input

#Cat > Filea < Fileb



tr-converting and deleting characters

Tr[option] ... Set1[set2]

Options:

-C or--complerment: a complement to the character set

-D or--delete: Deletes all characters belonging to the first character set

-S or-squeeze-repeats: the consecutive repeating character is represented by a single character

-T or--truncate-set1: Converts the corresponding character of the first character set to paragraph two character set corresponding to characters

Piping

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/85/55/wKiom1egQ6WwMkqsAAARuJaaItQ367.png "title=" 6.png " Style= "Float:none;" alt= "Wkiom1egq6wwmkqsaaarujaaitq367.png"/>

Pipe (using the symbol "|" Used to connect commands

Command 1 | Command 2 | Command 3 | ...

Send stdout of command 1 to stdin of command 2, stdout of command 2 to stdin of command 3

StdErr cannot be piped forward by default and can be implemented with 2>&1 or |&

The last command executes in the child shell process of the current shell process to

The ability to combine multiple tools

ls | Tr ' A-Z ' A-Z

Less: one page, one page to view the input:

$ ls-l/etc | Less

Mail: Send input by e-mail:

$ echo "Test Email" | Mail \ S "Test" [email protected]

Redirection and piping

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.