Shell重定向I / O命令 [英] Shell redirection i/o order

查看:114
本文介绍了Shell重定向I / O命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我玩的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命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆