shell--Input/Output redirection

Source: Internet
Author: User
Tags stdin

Transfer from http://www.cnblogs.com/chengmo/archive/2010/10/20/1855805.html

Before we get to the redirect, let's take a look at the Linux file descriptor.

Linux File Descriptor : Can be understood as a Linux trace open file, and the allocation of a number, this number is a bit like the C language operation of the file when the handle, through the handle can be implemented to read and write files. User can customize the file descriptor range is: 3-num, this maximum number, with the user's: Ulimit–n defined number has a relationship, cannot exceed the maximum value.

After Linux startup, 3 file descriptors will be opened by default: standard input 0, correct output 1, error output: Error outputs 2

After you open the file. The new file binding descriptor can be incremented in turn. A shell command executes, inheriting the file descriptor of the parent process. Therefore, all running shell commands will have a default of 3 file descriptors.

For any linux command execution, it would be such a process:

A command was executed:

There is one input: the input can be from the keyboard, or the file can be

Command execution completed: successful, will output to the screen: standard output default is the screen

Command execution error: The error is also output to the screen: standard error is the default also refers to the screen

The file input and output is done by tracing the integer handle of all open files for a given process. These numeric values are file descriptors. The most known file meter descriptors are stdin,stdout , and stderr, and the number of file descriptors is 0,1 and 2, respectively. These numbers and the respective equipment are reserved. Before a command executes, all input and output will be prepared by default (Stdin,stdout,stderr), and if an error occurs at this time, the command will be terminated and will not be executed. Command parsing process, you can refer to: Linux Shell wildcard characters, metacharacters, escape character usage examples introduced

These default output, the input is the Linux system, we in the process of use, sometimes do not want to execute the results output to the screen. I want to output to a file or other device. At this point we need to redirect the output.

The common input and output operators under the Linux shell are:

1. Standard input (stdin): code 0, using < or <</dev/stdin,/proc/self/fd/0 0 for:/dev/stdin
2. Standard output (STDOUT): Code 1, using > or >>/dev/stdout,/PROC/SELF/FD/1 1 for:/dev/stdout
3. Standard error Output (STDERR): Code 2, using 2> or 2>>;/dev/stderr,/PROC/SELF/FD/2 2 for:/dev/stderr

    • Output redirection:

Format:

command-line1 [1-n] > file or document operator or device

The above command means: The result of a command execution (standard output, or error output, originally to print to the screen above) redirect other output devices (files, open file operators, or printers, etc.) 1, 2 are standard output, error output.

Instance:

1234567891011121314151617181920212223242526272829303132333435363738 #显示当前目录文件 test.sh test1.sh test1.sh实际不存在[[email protected] shell]$ lstest.sh test1.shls: test1.sh: 没有这个文件和目录test.sh#正确输出与错误输出都显示在屏幕了,现在需要把正确输出写入suc.txt# 1>可以省略,不写,默认所至标准输出[[email protected] shell]$ lstest.sh test1.sh 1>suc.txtls: test1.sh: 没有这个文件和目录[[email protected] shell]$ catsuc.txt test.sh#把错误输出,不输出到屏幕,输出到err.txt[[email protected] shell]$ lstest.sh test1.sh 1>suc.txt 2>err.txt[[email protected] shell]$ catsuc.txt err.txt test.shls: test1.sh: 没有这个文件和目录#继续追加把输出写入suc.txt err.txt  “>>”追加操作符[[email protected] shell]$ lstest.sh test1.sh 1>>suc.txt 2>>err.txt #将错误输出信息关闭掉[[email protected] shell]$ lstest.sh test1.sh 2>&-test.sh[[email protected] shell]$ lstest.sh test1.sh 2>/dev/nulltest.sh#&[n] 代表是已经存在的文件描述符,&1 代表输出 &2代表错误输出 &-代表关闭与它绑定的描述符#/dev/null 这个设备,是linux 中黑洞设备,什么信息只要输出给这个设备,都会给吃掉 #关闭所有输出[[email protected] shell]$ lstest.sh test1.sh  1>&- 2>&- #关闭 1 ,2 文件描述符[[email protected] shell]$ lstest.sh test1.sh  2>/dev/null1>/dev/null#将1,2 输出转发给/dev/null设备 [[email protected] shell]$ lstest.sh test1.sh >/dev/null2>&1#将错误输出2 绑定给 正确输出 1,然后将 正确输出 发送给 /dev/null设备  这种常用<p>[[email protected] shell]$ lstest.sh test1.sh &>/dev/null#& 代表标准输出 ,错误输出 将所有标准输出与错误输出 输入到/dev/null文件</p>

Attention:

1, the shell encountered the ">" operator, will determine whether the right file exists, if it exists first delete, and create a new file. There is no direct creation. Whether or not the left command executes successfully. The file on the right will become empty.

2, ">>" operator, judge the right file, if not present, create first. Opening a file as added, assigns a file descriptor [not specifically specified, defaults to 1, 2] and then binds to the standard output (1) or Error output (2) on the left.

3, when the command: finished, the binding file descriptor is also automatically invalidated. 0,1,2 will be free again.

4, a command start, command input, correct output, error output, the default binding 0,1,2 file descriptor.

5, a command before execution, the first check the output is correct, if the output device error, will not be executed command

    • Input redirect

Format:

command-line [n] <file or file descriptor & device

The command defaults to the input from the keyboard, changed from the file, or other open file and device input. Execute this command to bind the standard input 0 to a file or device. will be entered by it.

Instance:

12345678910111213141516 [[email protected] shell]# cat > catfile testing catfiletest#这里按下 [ctrl]+d 离开 #从标准输入【键盘】获得数据,然后输出给catfile文件[[email protected] shell]$ cat>catfile <test.sh#cat 从test.sh 获得输入数据,然后输出给文件catfile [[email protected] shell]$ cat>catfile <<eoftesta filetest!eof#<< 这个连续两个小符号, 他代表的是『结束的输入字符』的意思。这样当空行输入eof字符,输入自动结束,不用ctrl+D

    • exec binding Redirection

Format:

exec file descriptor [n] < or > file or filename descriptor or device

In the above mentioned input, output redirection will be input, output binding file or device after. It is only valid for the current instruction. If required after binding, all subsequent commands are supported. You need to use the EXEC command.

Instance:

12345678910111213141516171819202122 [[email protected] shell]$ exec6>&1#将标准输出与fd 6绑定[[email protected] shell]$ ls/proc/self/fd/0  1  2  3  6#出现文件描述符6[[email protected] shell]$ exec1>suc.txt#将接下来所有命令标准输出,绑定到suc.txt文件(输出到该文件)[[email protected] shell]$ ls-al#执行命令,发现什么都不返回了,因为标准输出已经输出到suc.txt文件了[[email protected] shell]$ exec1>&6#恢复标准输出[[email protected] shell]$ exec6>&-#关闭fd 6描述符 [[email protected] ~]$ ls/proc/self/fd/0  1  2  3

Note: Save the standard input to the file descriptor 6 before use, the description below, the file descriptor opens by default 0,1,2 can also use the custom descriptor. The standard output is then bound to a file, and then all of the output will occur to the file. After use, restore the standard output, close open File descriptor 6.

Interesting things:

Maybe a friend will use this: Exec 1>suc.txt, and then all the output is bound to the Suc.txt file, so how to restore the original? Try it and you'll find the problem ...

    • A little more complicated instance
1234567891011 exec  3<> test .sh; #打开test. SH read-write operation with file descriptor 3 binding  while  read  line<&3 &NBSP; do       echo   $line; done #循环读取文件描述符3 (read test.sh content) exec  3>&- exec  3<&- #关闭文件的, input, output binding

    • Summary below:

Learning to summarize, the summary can be improved. Ha ha!

It is estimated that some friends are dizzy. How is the redirection of Linux so complex, but also the file open descriptor is read, as well as the default standard input output.

In fact, to summarize, the redirect application usually has the following two points:

1, reset the command of the default input, output, point to their own files (files, file descriptors, the device is actually a file, because Linux is based on the device is also a file, descriptor also point is a file, haha)

2. Extend your new descriptor to read and write files

shell--Input/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.