Unix shell input and output redirection

Source: Internet
Author: User
Tags stdin

When tapping the code, it is often possible to print some progress or log information properly to help us track the execution of the program. However, these results or the log printing information to the screen is not a basis for future inspection problems. This is the role of redirection, when tapping the code, we can easily connect the relevant print information to the screen or from the keyboard to receive input (the advantage is to avoid the direct operation of the file). With redirects we can easily redirect the input output to a file or somewhere else.

1. Descriptive descriptor of the document (the following sections are from Wikipedia)

The file Description Descriptor (document descriptor) is a term in computer science and is an abstraction that describes a reference to a file.

The descriptive descriptor of a file is a non-negative integer in form.

As a matter of fact. It is an index value that points to the record table in which the kernel opens a file for each process maintained by the process. When a program opens an existing file or creates a new file. The kernel returns a file description descriptor to the process.

In the program design. Some of the underlying programming tends to revolve around the file description descriptor unfolding.

However, the concept of descriptive descriptors is often applied only to UNIX and Linux operating systems.


There are two main strengths of the descriptive descriptors: the I/O operation based on the descriptive descriptor of the file is compatible with the POSIX standard; in Unix, Linux system calls. A large number of system calls are dependent on the file description descriptor.

There are two major drawbacks to the concept of file description descriptors: On non-unix/linux operating systems (such as Windows NT). cannot be programmed on the basis of this concept because the file descriptor is only an integer in form. When the amount of code increases, it makes it difficult for programmers to tell which integers mean data, and those that imply a descriptive descriptor of the file. As a result, the readability of the finished code becomes very poor. This is usually solved by eliminating magic numbers.

File I/O operations for standard libraries defined in the ANSI C specification. The ANSI C specification gives a workaround, which is to use a pointer to the file structure body. In fact, the implementation of the file structure on the Unix/linux platform is often encapsulated in the description of the document descriptor variables.

2. Input/Output redirection

2.1 stdin, stdout and stderr

Simply put, a file description descriptor is an integer associated with an open file or device that maintains an association with an open file or device.

The most common descriptive descriptors are stdin, stdout, and stderr, each of which are 0, 1, and 2, which are file descriptive descriptors reserved by the system, corresponding to standard input, standard output, and standard error.

The UNIX system opens these three file descriptors by default and associates the stdin to the keyboard, associating stdout and stderr to the screen. The main input and output redirection here is to redirect the three file descriptors once again to other files or devices we want. The main operators for Stdin,stdout and stderr redirection are <, >, and >> In the absence of the specified detailed file operator, the default is this: command < file.txt equals command 0 < File.txt, which means that the default is to redirect the file to the file descriptor 0. Command > file.txt is equivalent to command 1> file.txt, which means that the file Description descriptor 1 is redirected to a file by default. >> and > are the same. Suppose you want to redirect stderr. It is necessary to display the designation, for example command 2> file.txt. Output command error information to file.txt.

2.2 REDIRECT Input

To avoid entering data manually each time, we are able to write data to a file. The input is then redirected to the file using redirection. In order to demonstrate the use of the Linux Cat command, the Cat command assumes no parameters, reads the standard input and outputs it to the screen (in fact, the cat uses '-' to do the same). Cat-), effects such as the following:

The following writes the above contents to the file Test.txt. Then redirect the cat's input to the file, where "<" is used. This operator:

In fact, the cat 0<test.txt effect is the same as it says.

Of course. This is just a sample of cat. To view the contents of a file with cat why bother.

2.2 REDIRECT Output

2.2.1 REDIRECT standard input and standard error

In order to test the standard error, a new test folder was created, and then three files F1.txt, F2.txt, and F.txt. Write the above words in F.txt. The permissions for F1.txt and F2.txt are then set to chmod f1.txt. The following starts with the standard output and standard error redirection, respectively.

1. Before redirection [[email protected] test]$ cat f*cat:f1.txt:permission deniedcat:f2.txt:Permission Deniedhello, world! Ni hao!  I ' m unhappy.2, redirect standard output [[email protected] test]$ cat f* > Stdout.txtcat:f1.txt:Permission deniedcat:f2.txt:Permission Denied[[email protected] test]$ cat stdout.txt Hello, world! Ni hao! I ' m unhappy.3, redirect standard output and standard error [[email protected] test]$ cat f* 2> Stderr.txthello, world! Ni hao! I ' m unhappy. [email protected] test]$ cat stderr.txt cat:f1.txt:Permission deniedcat:f2.txt:Permission denied4, at the same time redirecting standard output to STD OUT.txt, redirect standard error to stderr.txt[[email protected] test]$ rm stderr.txt Stdout.txt [[email protected] test]$ cat f* 2> Stder R.txt >stdout.txt[[email protected] test]$ cat stderr.txt stdout.txt cat:f1.txt:Permission Deniedcat:f2.txt:Permiss Ion Deniedhello, world! Ni hao! I ' m unhappy.

Assuming that the output redirection file does not exist, the file is created by default, assuming that the file exists and has content. The original content will be emptied. Of course, if you want the new redirect content appended to the original content, you can change the above ' > ' to ' >> '. can achieve results.

2.2.2 REDIRECT standard output and standard error to a file at the same time

There are several ways to implement this, such as the following:

1, Cat f* &> stdall.txt[[email protected] test]$ cat f* &> stdall.txt[[email protected] test]$ cat Stdall.txt Cat:f1.txt:Permission deniedcat:f2.txt:Permission Deniedhello, world! Ni hao! I ' M unhappy.2, cat f* 1> stdall0.txt 2>&1[[email protected] test]$ cat f* 1> stdall0.txt 2>&1[[email PR Otected] test]$ cat stdall0.txt cat:f1.txt:Permission deniedcat:f2.txt:Permission Deniedhello, world! Ni hao! I ' m unhappy. Counter example: [[email protected] test]$ cat f* 2>&1 1> stdall0.txtcat:f1.txt:Permission deniedcat:f2.txt:Pe Rmission denied
Note that the above counter-example cannot be a cat f* 2>&1 1> stdall0.txt (due to the assumption that standard errors are first redirected to standard output.) When the standard output is not redirected, it is still output to the screen, which results in a standard error being redirected to the screen. ), and must be the cat f* 1> stdall0.txt 2>&1 (the standard output is redirected to a file first, all the content to the standard output is redirected to the file, so standard errors later redirected to standard output are also output to the file. )。

The above 1 uses a slightly special operator &> the ability to redirect standard output and standard errors at the same time, 2 is to redirect standard errors to standard output, and then redirect the standard output to achieve the goal. The implementation of the two ideas is slightly different.

The reason for this is to use cat f* 1> stdall0.txt 2>&1. The reason is to add a & in front of 1. Because 1 is a file descriptor created by default as a system, it should be referred to by &, which should be described below.

2.2.3 New Skill Get

(1) Discard unwanted output

There is a device file similar to the Trash bin:/dev/null. Redirecting the unwanted output to the file is possible.

1. Discard standard output [[email protected] test]$ cat f* >/dev/nullcat:f1.txt:permission deniedcat:f2.txt:Permission denied2, discard standard error error [[email protected] test]$ cat f* 2>/dev/nullhello, world! Ni hao!  I ' m unhappy.3, all discarded (the following three effects same) [[email protected] test]$ cat f* 2>/dev/null 1>/dev/null[[email protected] test]$ cat f* &>/dev/null [[email protected] test]$ cat f* 1>/dev/null 2>&1
(2) Save the output to the file, and let the output display on the screen

Sometimes. We want to archive the output of the program, and we want to see the output of the program (the redirection above doesn't see the output, only the files can be viewed later). Here, a command tee is used, which reads the contents of the standard input and then prints it both to the standard output and then to the file (Tee-read from standards input and write to standardized output and files). Note here that the output of the command needs to be sent to the pipeline. The tee is then used to receive the input of the pipeline, and only the content of the standard output can be piped. So first redirect the standard error to standard output.

[email protected] test]$ Cat f* 2>&1 | Tee tee.txtcat:f1.txt:Permission deniedcat:f2.txt:Permission Deniedhello, world! Ni hao! I ' m unhappy. [email protected] test]$ cat tee.txt cat:f1.txt:Permission deniedcat:f2.txt:Permission Deniedhello, world! Ni hao! I ' m unhappy.
This will create a nonexistent file on its own initiative. and empty the original content of the existing file, assuming that you want to use the Append mode, use cat f* 2>&1 | Tee-a Tee.txt.
3. Create your own defined file descriptive descriptors

0, 1 and 2 are file descriptive descriptors reserved by the system, and we are able to create our own defined file descriptors. There are three types of file description descriptors that you define: read-only mode, truncation mode, and append mode. Assume that you have created a file descriptor, num, that you can use &num to refer to it, and that is the reason for using &1 above.

3.1 Read-only mode

Use "exec num<filename" to create a read-only file descriptive narrative; A read-only file descriptor can only be read once. If two reads are required. It is necessary to create the descriptive descriptor again.

[[email protected] test]$ exec 5<f.txt[[email protected] test]$ cat <&5hello, world! Ni hao! I ' m unhappy. [[email protected] test]$ cat <&5//Two reads no matter what the output
3.2 Truncation Mode

Truncation mode refers to the time when a descriptive descriptor is created for the file. The original content in the corresponding file will be emptied. Use the "Exec num>filename" mode to create a truncated mode truncation mode after the file description descriptor is created. All output to the file description the contents of the descriptor are appended to the corresponding file, only the content of the file description before the creation of the narrative is emptied.

[email protected] test]$ cat f.txt Hello, world! Ni hao! I ' m unhappy. [[email protected] test]$ exec 7>f.txt[[email protected] test]$ cat f.txt [[email protected] test]$ echo "Love" >&am P;7[[email protected] test]$ echo "Happy" >&7[[email protected] test]$ cat F.txt lovehappy
3.3 Append Mode

Using "exec num>>filename" to create the file descriptor of the Append mode, and the truncation mode, when creating the descriptive descriptor of the file, the original content in the corresponding file will not be emptied, The contents of all output to the descriptive descriptor of the file are appended to the corresponding file in turn.

[[email protected] test]$ cat f.txt lovehappy[[email protected] test]$ exec 8>>f.txt[[email protected] test]$ cat F. TXT lovehappy[[email protected] test]$ echo "Friendship" >&7[[email protected] test]$ echo "Flair" >&7[[ema Il protected] test]$ cat F.txt Lovehappyfriendshipflair

All right. About the descriptive descriptor of the document is here today, after learning the new content will be re-fill.

Unix 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.