I've read the terms and conditions, but there's nothing related to the question like Stack Overflow, so I'm asking about Linux.
When I was reading a translation of The Linux Command Line (Complete Introduction), I saw a part called Standard Error Redirection, and I'm not sure what the file descriptor means. The following is the contents of the book:
Redirection operators are not required when overriding standard errors. However, refer to the file descriptor. The program may output any of the file streams designated by a number. We previously referred to standard input/output and standard errors, which Shell internally expresses as file descriptors 0, 1, and 2, respectively. The shell supports notation that can be overridden using the file descriptor number. This notation can be used when overriding the standard error because the standard error is the same as the file descriptor 2:
[me@linuxbox~]$ls -l/bin/usr2> ls-error.txt
File descriptor 2 is located right in front of the redirection operator and sends a standard error message to the ls-error.txt file.
What I don't understand is,
"However, refer to the file descriptor. The program may output any of the file streams designated by the code."Wow, "Shell internally represents them as file descriptors 0, 1, and 2, respectively.".
linux bash
Learning about system programming will help you understand it soon.
A file descriptor is a concept that, in the kernel of UNIX (Linux), processes use to manage the files they are dealing with. Standard C uses the FILE
structure, but system programming uses the concepts provided by the kernel provided by the operating system. On UNIX (Linux) operating systems, when a process opens a file, it gives an identifier to manage this file handle, which has an integer value. In other words, UNIX (Linux) calls the identifier (series number) for the file that is being opened and used a file descriptor, which is independent of each process.
Detailed information about the file descriptor exists inside the kernel, and the process handles the input and output of the file (or I/O, such as a network socket) through this file descriptor number.
First of all, every process is always opening three file streams. One is standard input as a stream to receive input from the keyboard. And the other two are standard output and standard error output. The file descriptors are assigned in the order we just talked about, and they are assigned in the order of 0, 1, and 2.
If you run ls
in the shell, a process called ls will be created in the Linux kernel to execute the program code, which, as mentioned earlier, is essentially using a 0,1,2 file descriptor.
The shell has features such as redirection or pipes that allow you to overwrite the default settings for these file streams with other file streams.
When ls > ls.txt
, redirects the standard output of the ls
process to a file called ls.txt
rather than to output the file descriptor number 1 to the screen.
Similarly, ls 2>ls-error.txt
redirects the ls
process to output file descriptor #2, which is the standard error output, to a file called ls-error.txt
instead of the screen.
Note: Processing redirection with the >
symbol is a function of the shell.
Why are all processes opening 3 file streams...
© 2024 OneMinuteCode. All rights reserved.