在UNIX中内部重定向的工作方式 [英] How redirection internally works in unix

查看:75
本文介绍了在UNIX中内部重定向的工作方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们举个例子: 我有一个名为a.out的可执行文件.它包含二进制信息,以将一些文本打印到printf的STDOUT cos. 所以当我给出./a.out时,我在控制台STDOUT上看到了printf的输出

Lets take an example : i have an executable named a.out. This contains binary information to print some text to STDOUT cos of printf. So when I give ./a.out, i see output of printf at the console STDOUT

说如果我在控制台中执行"./a.out> tempFile".这是如何运作的?由于a.out内部有printf,因此理想情况下,i除了要在STDOUT中打印的文本之外.重定向如何消耗此文本,为什么我们在控制台中看不到任何输出,而仅在文件中看到printf文本

Say if i do './a.out > tempFile' in console. How does this work? Since there is printf inside a.out, ideally i except the text to be printed in STDOUT. How does redirection consume this text and why do we not see any output in console and only in the file we see the printf text

推荐答案

在UNIX中,一切是文件.默认情况下,所有stdout都是(例如)/dev/tty文件,该文件是连接到控制台/终端/窗口的设备驱动程序.输出只是发送到该文件(设备驱动程序),这会导致将其输出到您用于交互式I/O的任何文件.

In UNIX, everything is a file. All stdout is by default is the (for example) /dev/tty file which is a device driver hooked up to your console/terminal/window. Output is just sent to that file (device driver) which causes it to be output to whatever you're using for interactive I/O.

a.out >xyzzy.txt这样的所有a命令首先都是将程序的标准输出而不是/dev/tty连接到该文件,因此输出显示在那里.

All the a command like a.out >xyzzy.txt does is first connect the standard output of the program to that file rather than /dev/tty, hence the output shows up there instead.

这篇关于在UNIX中内部重定向的工作方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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