Linux uses pipelines, IO redirection

Source: Internet
Author: User

The program is composed of data + instructions, in the program design, by default from the standard input (standard-input) read data, to the standard output (standard-output) output data, program execution error is output to the standard error output (STANDARD-ERROR)

Standard input for the file descriptor 0

Output 1

Error Output 2

IO redirection is the redirection of data streams that the program should 0.1.2 read or output to other files

> Standard output redirection

> filename, redirects the data stream that should have been output to the standard output (monitor) to the filename file, overwriting the contents of the file if it already has content

The set command is used to view and define the variables for the shell. Where set + C means to prohibit overwriting of existing files (note: If the file exists, regardless of whether the file has any contents, it is forbidden to overwrite) set-c to turn off this feature (default)

>> Append output redirection

>>filename if the content already exists in the filename file. Do not overwrite, but append to the file

Note: The above two methods only redirect the standard output, if the program execution error, will still output to the standard error output, because the file descriptor is 1 and 2, so belong to a different data flow

2> REDIRECT standard error output data stream

2>filename, if the program executes an error, directs its output to the filename file

2>>

&> REDIRECT Output

&>filename, the standard output and the standard error output are directed to the file

&>>filename

Sometimes >/dev/null 2>&1 is often seen in scripts: it means redirecting standard output to an empty device file, 2>&1 indicating that the standard error output is equivalent to standard output, and also directed/dev/null

Tee command can be implemented to save the results of the command to the file and output to a screen, with the help of the pipeline implementation

Command | Tee filename

String Substitution command

1, TR, commonly used in pipeline operation. Replaces a specific string of output content.

# Command | TR ' original string ' replace string '

# Command | tr-d ' to delete string '-D Delete string

2. Sort by a specific string

#sort [-tkn] File name

-T SET field separator

-K Sets the selected field

-N Using numeric sorting

Filter duplicate Data--uniq

Uniq is used to filter adjacent, duplicated data in the output content, preserving only one of them.

# Command | uniq

# command |uniq-c to count the rows found

Xargs: The command to be processed after the output of the first command is given to Xargs.

Example: LS A * | Tee file1 |xargs rm–t

Example 2: File deletion prompt

Read-p "Please enter the file you want to delete:" A;

LS $A | Teefile1 | Xargs Rm–f | Echo ' Cutfile1 ' has been removed | Echo ' > File1



< input redirection

in Linux, there are many programs that need to read data from standard inputs, and input redirection is a standard input source for changing a program

WC </etc/fstab

<

<< no append standard input one says,<< is used to generate documents

Cat << EOF will appear with an input prompt, the user input will be displayed as a file, EOF is just a file terminator (can be implemented as a different character), indicating that this is a file

cat >>/tmp/filename <<eof means that the generated document is then saved to a file as output redirection, which is commonly used in scripts to generate documents


Pipe: Standard output of one command as standard input for another command

Cat/etc/issue | Tr ' A-Z ' A-Z

This article is from the "Zxcvbnm Xuan ye" blog, please be sure to keep this source http://10764546.blog.51cto.com/10754546/1702064

Linux uses pipelines, IO redirection

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.