Standard input, output, and error for Linux shells

Source: Internet
Author: User
Tags echo command

when compiling a file, if the compilation process is long, you can output standard error redirection to a file 2 > 1.txt can also be redirected to standard output by pipe 2 > &1standard input, output, and error for Linux shellsTags: linux shellshell redirection2013-08-19 20:42 30403 People read comments (0) favorite reports Classification:system operation and Maintenance (a)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

1. File descriptors

When a Linux shell executes a command, each process is associated with three open files and uses file descriptors to refer to them. Because the file descriptor is not easy to remember, the shell also gives the corresponding file name:

File File descriptor
Input file-Standard input 0 (default is keyboard, 0 o'clock is the output of file or other command)
Output file-standard output 1 (default is screen, 1 o'clock is file)
Error output file-standard error 2 (default is screen, 2 o'clock is file)
There are actually 12 file descriptors in the system, and we can use the file descriptor 3 to 9 arbitrarily.

2. File redirection: Change the input source and output location of program operation

2.1. Output redirection:

Command > FileName REDIRECT standard output to a new file
Command >> filename REDIRECT standard output to a file (append)
Command > FileName REDIRECT standard output to a file
Command > FileName 2>&1 redirect standard output and errors together into a file
Command 2 > FileName redirect standard errors to a file
Command 2 >> filename REDIRECT standard output to a file (append)
Command >> filename2>&1 redirect standard output and errors together to a file (append)

2.2. Input redirection:

Command < filename > filename2 command commands use the filename file as the standard input, with the filename2 file as the standard output
Command < filename command commands to use the filename file as the standard input
Command << Delimiter Read in from standard input, know to encounter delimiter delimiter

2.3. Binding redirection

Command >&m redirect standard output to file descriptor M
Command < &- Turn off standard input
Command 0>&- Ditto


Some advanced uses of 3.shell redirection

3.1. REDIRECT standard error

Example 1:
Command 2>/dev/null
redirect incorrect information to an empty device if command execution is faulted
Example 2:
Command > Out.put 2>&1
REDIRECT the standard output and standard errors of command execution to Out.put (that is, the output is printed to out.put regardless of whether the command is executed correctly or incorrectly).

3.2.exec usage
The EXEC command can be used to replace the current shell, in other words, the child shell is not started, and any existing environment variables will be purged when using this command, and a shell (re-enter the user name and password) is restarted.
EXEC command
Where command is usually a shell script.
For file descriptor operations (and only this time), it will not overwrite your current shell


Example 1:
#!/bin/bash
#file_desc


EXEC 3<&0 0<name.txt
Read line1
Read Line2
EXEC 0<&3
echo $line 1
Echo $line 2


which
First, exec 3<&0 0< Name.txt means redirecting the standard input to the file descriptor 3 (0 for standard input) and then redirecting the file Name.txt content to the file descriptor 0, which in effect redirects the contents of the file Name.txt to the file descriptor 3. Then open file descriptor 3 via exec;

Then, through the Read command reading the first line of name.txt content line1, the second line of content line2, through the exec 0<&3 close the file descriptor 3;

Finally, use the echo command to output line1 and line2. It's a good idea to run this script at the terminal and try it yourself.


Example 2:
EXEC 3<>test.sh;
#打开test. SH read-write operation, binding with file descriptor 3

While Read line<&3
Do
Echo $line;
Done
#循环读取文件描述符3 (read test.sh content)
EXEC 3>&-
EXEC 3<&-
#关闭文件的, input, output binding

Reference:

http://http://blog.csdn.net/cjfeii/article/details/10084343

http://zqwt.012.blog.163.com/blog/static/1204468420103272857116/

Http://www.cnblogs.com/chengmo/archive/2010/10/20/1855805.html

http://blog.csdn.net/flowingflying/article/details/5201199

Standard input, output, and error for Linux shells

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.