对标准输入、标准输出和标准错误感到困惑? [英] Confused about stdin, stdout and stderr?

查看:23
本文介绍了对标准输入、标准输出和标准错误感到困惑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这三个文件的用途感到很困惑.如果我的理解是正确的,stdin 是程序写入其在进程中运行任务的请求的文件,stdout 是内核将其写入的文件输出和请求它的进程从中访问信息,stderr 是输入所有异常的文件.在打开这些文件以检查这些文件是否确实发生时,我发现似乎没有任何提示!

I am rather confused with the purpose of these three files. If my understanding is correct, stdin is the file in which a program writes into its requests to run a task in the process, stdout is the file into which the kernel writes its output and the process requesting it accesses the information from, and stderr is the file into which all the exceptions are entered. On opening these files to check whether these actually do occur, I found nothing seem to suggest so!

我想知道的是这些文件的目的到底是什么,用很少的技术术语绝对愚蠢的回答!

What I would want to know is what exactly is the purpose of these files, absolutely dumbed down answer with very little tech jargon!

推荐答案

标准输入 - 这是您的进程读取以从您那里获取信息的文件句柄.

Standard input - this is the file handle that your process reads to get information from you.

标准输出 - 您的进程将常规输出写入此文件句柄.

Standard output - your process writes conventional output to this file handle.

标准错误 - 您的进程将诊断输出写入此文件句柄.

Standard error - your process writes diagnostic output to this file handle.

这是我能做到的最愚蠢的:-)

That's about as dumbed-down as I can make it :-)

当然,这主要是约定俗成的.如果您愿意,没有什么可以阻止您将诊断信息写入标准输出.您甚至可以完全关闭三个文件句柄并打开自己的文件进行 I/O.

Of course, that's mostly by convention. There's nothing stopping you from writing your diagnostic information to standard output if you wish. You can even close the three file handles totally and open your own files for I/O.

当您的进程启动时,它应该已经打开了这些句柄,并且可以读取和/或写入它们.

When your process starts, it should already have these handles open and it can just read from and/or write to them.

默认情况下,它们可能连接到您的终端设备(例如,/dev/tty),但 shell 将允许您在这些句柄和特定文件和/或设备之间建立连接(甚至到其他进程的管道)在您的流程开始之前(一些可能的操作相当聪明).

By default, they're probably connected to your terminal device (e.g., /dev/tty) but shells will allow you to set up connections between these handles and specific files and/or devices (or even pipelines to other processes) before your process starts (some of the manipulations possible are rather clever).

一个例子是:

my_prog <inputfile 2>errorfile | grep XYZ

这将:

  • my_prog 创建一个进程.
  • 打开 inputfile 作为标准输入(文件句柄 0).
  • 打开 errorfile 作为标准错误(文件句柄 2).
  • grep创建另一个进程.
  • my_prog 的标准输出附加到 grep 的标准输入.
  • create a process for my_prog.
  • open inputfile as your standard input (file handle 0).
  • open errorfile as your standard error (file handle 2).
  • create another process for grep.
  • attach the standard output of my_prog to the standard input of grep.

重新评论:

当我在/dev 文件夹中打开这些文件时,为什么看不到正在运行的进程的输出?

When I open these files in /dev folder, how come I never get to see the output of a process running?

这是因为它们不是普通文件.尽管 UNIX 将所有内容都作为文件系统中某处的文件呈现,但在最低级别上却并非如此./dev 层次结构中的大多数文件是字符设备或块设备,实际上是设备驱动程序.它们没有大小,但有主要和次要设备号.

It's because they're not normal files. While UNIX presents everything as a file in a file system somewhere, that doesn't make it so at the lowest levels. Most files in the /dev hierarchy are either character or block devices, effectively a device driver. They don't have a size but they do have a major and minor device number.

当您打开它们时,您连接的是设备驱动程序而不是物理文件,并且设备驱动程序足够聪明,知道应该单独处理单独的进程.

When you open them, you're connected to the device driver rather than a physical file, and the device driver is smart enough to know that separate processes should be handled separately.

Linux /proc 文件系统也是如此.这些不是真正的文件,只是对内核信息进行严格控制的网关.

The same is true for the Linux /proc filesystem. Those aren't real files, just tightly controlled gateways to kernel information.

这篇关于对标准输入、标准输出和标准错误感到困惑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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