Linux file descriptors and redirection
14:08 by pursuer.chen 2016-04-12, 4191 reading, 8 comments, favorites , edit
Introduction
File descriptor is the file input and output integer associated, when writing scripts often use standard file descriptors will redirect the output to the content, 0,1,2 is the file descriptor (corresponding to stdin, stdout, stderr ), <,>, called the operator >>.
concept
stdin (0): standard input, the concept somewhat less easily understood example: 1. Use <reads from the file, the contents of the current command 2 is piped to the next command and the next command, and the actual content. Therefore stdin transmitted to the next command is read from stdin.
stdout (1): standard output; This is the default option. Usage: 1> is equivalent to> or an equivalent >> >> ,; if you want to use other file descriptor, the descriptor must be placed before the operator.
stderr (2): standard error, use 2> or 2 >>, standard error may be inserted into the file without error message displayed on the terminal.
<: Read from the file.
>: The content into a file, empty the contents of the file before each will be inserted.
>>: the content into a file, the contents append to the existing file.
Examples
Generate test data
echo "hello word" > test1 cp test1 test2 chmod 000 test2
stdin(0)
1. read the content from the text
cat <test1
2. The read content is piped to the next command
cat test1 |tr -t 'a-z' 'A-Z' >test1.new
stdin(1)
The content redirected to a file
echo "this is stdout 1" >std1
Append to file
stderr(2)
Encountered an error when the terminal will display an error, the error message may be written to a file not to display terminal
While stdout and stderr information is inserted into the file, use &
The error messages are directed to standard output
echo "name" 2>&1 >> a or echo "name" >> a 2>&1
Internal redirect script text block, cat << EOF >> log.txt EOF
Custom file descriptor
You may also need to use a custom file descriptor exec; <,>, >> and meaning explained earlier, when calling the custom descriptor A prefix & custom descriptor.
1. Custom stdin, will be defined as 3 stdin read from the file, and then you can call the 3, 3 calls and direct calls the file result is the same, somewhat similar assignment.
exec 3<test1
2. Custom stdout, test results showed that the use of the custom descriptor> write data to the file and will not be repeated before emptying the contents, but in the standard descriptor is written will clear again.
to sum up
In the script file descriptor is used very frequently, commonly used method is to use the standard output and standard error.
Introduction
File descriptor is the file input and output integer associated, when writing scripts often use standard file descriptors will redirect the output to the content, 0,1,2 is the file descriptor (corresponding to stdin, stdout, stderr ), <,>, called the operator >>.
concept
stdin (0): standard input, the concept somewhat less easily understood example: 1. Use <reads from the file, the contents of the current command 2 is piped to the next command and the next command, and the actual content. Therefore stdin transmitted to the next command is read from stdin.
stdout (1): standard output; This is the default option. Usage: 1> is equivalent to> or an equivalent >> >> ,; if you want to use other file descriptor, the descriptor must be placed before the operator.
stderr (2): standard error, use 2> or 2 >>, standard error may be inserted into the file without error message displayed on the terminal.
<: Read from the file.
>: The content into a file, empty the contents of the file before each will be inserted.
>>: the content into a file, the contents append to the existing file.
Examples
Generate test data
echo "hello word" > test1 cp test1 test2 chmod 000 test2
stdin(0)
1. read the content from the text
cat <test1
2. The read content is piped to the next command
cat test1 |tr -t 'a-z' 'A-Z' >test1.new
stdin(1)
The content redirected to a file
echo "this is stdout 1" >std1
Append to file
stderr(2)
Encountered an error when the terminal will display an error, the error message may be written to a file not to display terminal
While stdout and stderr information is inserted into the file, use &
The error messages are directed to standard output
echo "name" 2>&1 >> a or echo "name" >> a 2>&1
Internal redirect script text block, cat << EOF >> log.txt EOF
Custom file descriptor
You may also need to use a custom file descriptor exec; <,>, >> and meaning explained earlier, when calling the custom descriptor A prefix & custom descriptor.
1.自定义stdin,将3定义为stdin从文件中读取内容,然后就可以调用3了,调用3和直接调用文件结果是一样的,有点类似赋值。
exec 3<test1
2.自定义stdout,测试结果发现在自定义描述符中使用>往文件中重复写数据并不会清空之前的内容,但是在标准的描述符中是会清空再写入的。
总结
文件描述符在脚本中使用的非常频繁,常用的使用方法就是标准输出和标准错误。