Linux Shell Data redirection

Source: Internet
Author: User
Tags stdin

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]$ ls test.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]$ ls test.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]$ ls test.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

Linux Shell Data 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.