In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to understand the meaning and difference of shell 1 > & 22 > & 1 & > filename redirection". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
When I saw "> & 1" and "> & 2" in shell, I never understood what it meant. The puzzle was solved by searching on the Internet. In fact, these are two kinds of output.
In shell programs, there are about three most commonly used FD (file descriptor), which are:
0 is a file descriptor that represents standard input (stdin)
1 is a file descriptor that represents standard output (stdout)
2 is a file descriptor that indicates standard error (stderr)
In a standard case, these FD are associated with the following devices:
Stdin (0): keyboard keyboard input and return at the front end
Stdout (1): the correct return value of monitor is output to the front end
Stderr (2): monitor error return value output to the front end
Give an example:
There is only one file a.txt in the current directory.
[root@redhat box] # ls
A.txt
[root@redhat box] # ls a.txt b.txt
Ls: b.txt: No such file or directory returns an error value because there is no b.txt file, which is called 2 output
A.txt, and this is the so-called 1 output
Let's move on to:
[root@redhat box] # ls a.txt b.txt 1 > file.out 2 > file.err
After execution, there is no return value. The reason is that the return values are redirected to the corresponding file instead of being displayed at the front end.
[root@redhat box] # cat file.out
A.txt
[root@redhat box] # cat file.err
Ls: b.txt: No such file or directory
Generally speaking, "1 >" can usually be omitted as ">".
You can write the above command as: ls a.txt b.txt > file.out 2 > file.err
Only with these understandings can we understand "1 > & 2" and "2 > & 1".
1 > & 2 the correct return value is passed to the 2 output channel & 2 represents the 2 output channel
If it is mistakenly written as 1 > 2, it means that the output of 1 is redirected to file 2.
The 2 > & 1 error return value is passed to the 1 output channel, and the & 1 represents the 1 output channel.
For instance。
[root@redhat box] # ls a.txt b.txt 1 > file.out 2 > & 1
[root@redhat box] # cat file.out
Ls: b.txt: No such file or directory
A.txt
Now, both the correct output and the wrong output are directed to the file.out file instead of being displayed at the front end.
In addition, there are not only 1 and 2 outputs, but also other types, which are only the most commonly used and basic.
> is a redirector, which redirects the previous output to the location specified later, such as (example 1):
Echo "some content" > filename.txt
The above example writes "something" to the filename.txt file.
> you can add a number to indicate what is redirected to the file, and the standard output is redirected to the file by default, so the following example is the same as the one above (example 2):
Echo "some content" 1 > filename.txt
If it is an error message, it will not be output to filename.txt (example 3):
$ls nodir 1 > filename.txt$ ls: nodir: No such file or directory
Nodir does not exist in the above example, so when querying through the ls command, the error message will be output to 2 (stderr), but what we specify is to redirect 1 to filename.txt, so after the above command is executed, there is no content in filename.txt. But executing the following command will write the error message to filename.txt (example 4):
$ls nodir 2 > filename.txt$ cat filename.txt$ ls: nodir: No such file or directory
& is a descriptor, and if you don't add & before 1 or 2, it will be treated as a normal file.
1 > & 2 means to redirect standard output to standard error.
2 > & 1 means to redirect standard error output to standard output.
& > filename means to redirect both standard output and standard error output to the file filename
Let's look at another example (listing 5):
$ls nodir 1 > filename.txt 2 > & 1$ cat filename.txt$ ls: nodir: No such file or directory
The above example redirects the standard output to the file filename.txt, and then redirects the standard error to the standard output, so the final error information is also written into the file through the standard output.
The following is what Baidu knows, you can refer to it:
Question: how to understand Linux redirection > & 2?
Question to add: echo "aaaaaaaaaaaaaaaa" > & 2 how to understand?
A:
> & 2, that is, 1 > & 2, that is, output the result to the same as standard error; if a standard error was redirected to a log file before, the standard output will also be redirected to this log file
For example: ls 2 > A1 > & 2 (equivalent to ls > A1 > & 1)
Redirect both standard output and standard error to A1, and no information can be seen on the terminal.
This is the end of the content of "how to understand the meaning and difference of shell 1 > & 22 > & 1 & > filename redirection". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.