MAC Command Learning-standard input and output redirection

Source: Internet
Author: User

Preliminary knowledge file descriptor (FileName descriptor)

In UNIX systems, file descriptors are an abstract concept for accessing files, input and output, pipelines, and network sockets [3]. The file descriptor is a non-negative positive integer, and each UNIX process will have three POSIX file descriptors corresponding to the standard stream, respectively:

    1. stdin-standard input, supplied to the application's standard input. The file descriptor is 0.
    2. stdout-standard output, the output of the program, in the case of non-redirection for the terminal. The file descriptor is 1.
    3. stderr-Standard error output, program error output. The file descriptor is 2.

For more information about file descriptors, see article {% post_link file_descriptor_101 file Descriptor 101}

Output redirection

There are two ways to redirect output:

    • n> files redirects the file descriptor N to the file, creates a file if it does not exist, and overwrites the original content if it exists.

    • n>> files redirect the file descriptor N to the file, and if the file does not exist, it will be created and, if present, will be written to the output at the end of the file.

If we do not specify a file descriptor, the default is 1, which is standard output.

Redirect to a different file

We can put stdout and stderr output to different files, examples:

zhengyi$ ls    file1 file2 file3zhengyi$ ls f* e* 1>out.txt 2>err.txtzhengyi$ cat out.txt     file1    file2    file3zhengyi$ cat err.txt     ls: e*: No such file or directory
Redirect to a file

In many cases we need to export stdout and stderr output to the same file, and this time we can use &> or &>> , they will output the standard output and standard error together.

Another way is to redirect one output to another by using m>&n it. It is important to note that the order in which they are used, such as the following example: zhengyi$ ls f* z* 1>out.txt 2>&1 zhengyi$ cat out.txt ls: z*: No such file or directory file1 file2 file3 zhengyi$ ls f* z* 2>&1 1>out.txt ls: z*: No such file or directory zhengyi$ cat out.txt file1 file2 file3

ls f* z* 1>out.txt 2>&1This example is to redirect the standard output to the file OUT.txt, and then redirect the standard error to standard output, which is working. ls f* z* 2>&1 1>out.txtin this example, the standard error is redirected to the standard input, at which point the standard input points to the default standard input, and when the standard input is later redirected to the file OUT.txt, the output target of the standard error is not changed.

Ignore some output

For example, if we want to filter the error in the output, we can output the standard error to /dev/null medium.

zhengyi$ ls f* z*    ls: z*: No such file or directory    file1 file2 file3zhengyi$ ls f* z* 2>/dev/null     file1 file2 file3
Input redirect

In contrast to output redirection, input redirection is expressed using the "<" notation. Example:

zhengyi$ cat out.txt     err.txt    file1    file2    file3    out.txtzhengyi$ sort -r < out.txt     out.txt    file3    file2    file1    err.txt

Output redirection can be combined with an input redirect, such as:

zhengyi$ sort -r < out.txt > sorted.txt

This is the out.txt as input and then redirects the results of sort to sorted.txt. When using both input and output redirection, it is important to avoid using the same file as much as possible. For example, if you point to the same file, the first output redirect will empty the OUT.txt file.

Resources
    1. Learn Linux, 101:streams, pipes, and redirects
    2. I/O redirection
    3. Wikipedia-file Descriptor

Original link

MAC Command Learning-standard input and output 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.