问题描述
我玩的I / O shell重定向。我尝试过的命令(在bash):
I'm playing with i/o shell redirection. The commands I've tried (in bash):
ls -al *.xyz 2>&1 1> files.lst
和
ls -al *.xyz 1> files.lst 2>&1
有是当前文件夹中没有任何 * XYZ
文件。
There is no any *.xyz
file in current folder.
这些命令给了我不同的结果。第一个命令显示错误消息 LS:名为* .xyz:在屏幕上没有这样的文件或目录
。但第二个输出该错误消息给该文件。为什么第一个命令未能写ERR输出到文件?
These commands gives me the different results. The first command shows an error message ls: *.xyz: No such file or directory
on the screen. But the second one prints this error message to the file. Why did the first command failed to write an err output to the file?
推荐答案
此错误:
ls: *.xyz: No such file or directory
是由 LS写在
二进制文件。标准错误
is being written on stderr
by ls
binary.
不过,在这个命令:
ls -al *.xyz 2>&1 1> files.lst
你先重定向 标准错误
到标准输出
在默认情况下转到的tty
(终端)
You're first redirecting stderr
to stdout
which by default goes to tty
(terminal)
然后,你要重定向标准输出
到文件 FILES.LST
,但是请记住,stderr并不重定向到文件,因为你有标准错误
到标准输出
重定向之前 标准输出
到文件
重定向。你的标准错误
仍然得到在这种情况下写入的tty
。
And then you're redirecting stdout
to a file files.lst
, however remember that stderr doesn't redirected to file since you have stderr
to stdout
redirection before stdout
to file
redirection. Your stderr
still gets written to tty
in this case.
然而,在第二种情况更改重定向(第一标准输出
的顺序文件
然后标准错误
到标准输出
),并正确地重定向标准错误
到文件
这也被使用标准输出
。
However in 2nd case you change the order of redirections (first stdout
to file
and then stderr
to stdout
) and that rightly redirects stderr
to a file
which is also being used by stdout
.
这篇关于Shell重定向I / O命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!