Experiment Understanding Linux SHELL input and output redirection

Source: Internet
Author: User

What is the difference between this command and 2>&1 >/dev/null when you find a statement in the shell script that >/dev/null 2>&1 has not previously understood the purpose of this command? The emphasis of these two commands and the shell input and output redirection is understood experimentally.






Shell Redirection Introduction



We generally use the shell command, mostly through the keyboard input, on the screen to see the command execution results (including normal output and error output). If, in some cases, we need to store the execution result of the shell command in a file, we need to use the redirection of the input and output.






File descriptor



When you execute a shell command, 3 files are opened by default, and each file has a corresponding file descriptor to facilitate our use:


file descriptor default   Corresponding file handle position
Standard input 0 Getting input from the keyboard /proc/slef/fd/0
Standard output 1 Output to screen (i.e. console) /proc/slef/fd/1
Bad output (Error outputs) 2 Output to screen (i.e. console) /proc/slef/fd/2





So we usually get input from the keyboard by default in executing the shell command, and the result is output to the console. But we can redirect the input and output by changing the default point of the file descriptor.






Output redirection



The use of output redirection is simple, and some basic commands are as follows:



Command Introduction



Command >filename redirect the standard output to a new file



Command 1>filename ibid.



Command >>filename append the standard output to the file



Command 1>>filename ibid.



Command 2>filename redirect the standard error to a new file



Command 2>>filename append the standard error to the new file






We use > or >> to redirect the output. The left side of the symbol represents the file descriptor, and if not, represents 1, which is the standard output, and the right side of the symbol can be a file or an output device. When using >, it will be judged that the right file does not exist, if it exists, it will be deleted first, then create a new file, if it does not exist, then create it directly. However, when you use >> to append, the original existing file is not deleted.






Let's take a look at the following example: we create a test directory with only one a.txt file under the directory.


[email protected] test]# Touch a.txt
[email protected] test]# ls a.txt b.txt
Ls:cannot access b.txt:no such file or directory
A.txt
[Email protected] test]# 


After LS, the standard output (a.txt) and error output (ls:cannot access b.txt:no such file or directory) are output to the screen.






[[email protected] test]# ls a.txt b.txt 1>out.txt



Ls:cannot access b.txt:no such file or directory



[email protected] test]# cat OUT.txt



A.txt



[[email protected] test]# ls a.txt b.txt 1>>out.txt



Ls:cannot access b.txt:no such file or directory



[email protected] test]# cat OUT.txt



A.txt



A.txt



In the above command, we redirect the original standard output to the out file, so the console is left with only error hints. And when the append operation is performed, the contents of the out file are not emptied, but there is one more a.txt.






To redirect the error output to a file



[[email protected] test]# ls a.txt b.txt 2>err.txt



A.txt



[email protected] test]# cat Err.txt



Ls:cannot access b.txt:no such file or directory



[[email protected] test]# ls a.txt b.txt >out.txt 2>err.txt



[email protected] test]# cat OUT.txt



A.txt



[email protected] test]# cat Err.txt



Ls:cannot access b.txt:no such file or directory



[Email protected] test]#


here, you may find>out 2>errand the one we mentioned at the beginning>/dev/null 2>&1It's a lot like that.





Input redirect



After understanding the output redirection, it is much easier to understand the input redirection. The basic commands for input redirection are as follows:






Command Introduction



Command <filename with the filename file as the standard input



Command 0<filename ibid.



Command <<delimiter reads from the standard input until the delimiter delimiter is encountered






We use < to redirect the input, if there is no write value on the left of the symbol, then the default is 0 (from the console input).






# Cat



123



123



Test



Test



^c



We can use the input redirection to write the characters we typed on the keyboard to the file. We need to use CTRL + C to end the input.






[email protected] test]# cat >out.txt



123



Test



Ddd



^c



[email protected] test]# cat OUT.txt



123



Test



Ddd



[Email protected] test]#






Then use the input redirection to have the cat read a file directly (see Cat output for the contents of the file):



[Email protected] test]# vim Input.txt



[email protected] test]# cat Input.txt



Aaa



Aaa



111



222



[email protected] test]# cat >out.txt <input.txt



[email protected] test]# cat OUT.txt



Aaa



Aaa



111



222



[Email protected] test]#



From input, the contents of the out file are replaced with the contents of the input file. So what's the role of <<? Let's look again:






[email protected] test]# cat >out.txt <<end



> 123



> 456



> JJJ



> FFF



> End



[Email protected] test]#



When you finish typing the cat >out <<end and then hit enter, the command does not end, and the cat command is like waiting for you to enter data, and when you type end, the Cat command ends. The characters entered before end have been written to the out file. This is the function of the input separator.









REDIRECT Bindings



With the above knowledge, let's look at the >/dev/null 2>&1 mentioned at the beginning. This command is actually divided into two commands, one is >/dev/null and the other is 2>&1.






1. >/dev/null



The purpose of this command is to redirect the standard output 1 to/dev/null. /dev/null represents an empty device file for Linux, and all content written to this file will be lost, commonly known as "black holes". After executing the >/dev/null, the standard output will no longer exist, and there is no place to find the output content.






2.2>&1



This command uses the redirect binding , with & to bind two outputs together. The function of this command is that the error output will be the same as the standard output with a file descriptor, which is error output and standard output output to the same file.






Before executing the shell command, Linux determines all the input and output locations and executes the redirected commands from left to right, so the >/dev/null 2>&1 is to redirect the standard output to/dev/null (discarding the standard output). Then the error output is redirected to the/dev/null due to the reuse of the standard output descriptor, and the error output is also discarded. After executing this command, the shell command will not output any information to the console, and no information will be output to the file.






>/dev/null 2>&1 VS 2>&1 >/dev/null



These two commands seem to be equivalent, but in fact they are different. Before executing the shell command, Linux determines all the input and output locations and executes the redirected commands from left to right. Then we also analyze from left to right in two steps:






2>&1>/dev/null:


    • 2>&1, bind the error output to the standard output. Because the standard output at this point is the default value, which is output to the screen, the error output is output to the screen.

    • >/dev/null, redirect standard output 1 to/dev/null.





Command standard output error Output



>/dev/null 2>&1 Discard Discard



2>&1 >/dev/null Discard screen






>/dev/null 2>&1 VS >/dev/null 2>/dev/null






So why use redirect binding instead of repeating it like >/dev/null 2>/dev/null.






To differentiate between the two, try to direct both the standard output and the error output to the out file:



[[email protected] test]# ls a.txt b.txt >out.txt 2>out.txt



[email protected] test]# cat OUT.txt



A.txt



Nnot access b.txt:no such file or directory output overwrite or garbled



[Email protected] test]#



There is output overwrite or garbled! as a result of this notation, standard output and error output will preempt the pipeline to the out file, so it may cause missing, overwritten, and so on when the content is output. Now there is coverage, and sometimes there is the possibility of only error messages or only normal information. In any case, the final situation cannot be estimated by using this notation.






Moreover, since the out file was opened two times, two file descriptors will be preempted to the file output content, so the overall IO efficiency is not as high as >/dev/null 2>&1.






Summarize



The above main describes the principle of Linux redirection and some basic commands, through experiments to understand the shell input and output of some points.






This article from "Wei_y_y Space" blog, declined reprint!



Experiment Understanding Linux SHELL input and 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.