没有 FILE 参数的 wc 命令(从标准输入读取)

wc command with no FILE argument (reading from standard input)

关于 'wc'(字数统计)命令...我试图理解以下内容(来自我在下面引用的 'man wc')"With no FILE, or when FILE is -, read standard input."

这究竟是如何工作的?我在什么时候输入“标准输入”?

网上找的解释比较乱。也许我只是缺少一些关于标准输入到底是什么的基本信息。

来自'man wc'

SYNOPSIS wc [OPTION]... [FILE]... wc [OPTION]... --files0-from=F

DESCRIPTION Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. A word is a non-zero-length sequence of characters delimited by white space.

   With no FILE, or when FILE is -, read standard input.

引用 man 3 标准输入:

       Under  normal circumstances every UNIX program has three streams opened
       for it when it starts up, one for input, one for output,  and  one  for
       printing diagnostic or error messages.  These are typically attached to
       the user's terminal (see tty(4)) but might instead refer  to  files  or
       other  devices,  depending  on what the parent process chose to set up.
       (See also the "Redirection" section of sh(1).)

       The input stream is referred to as "standard input"; the output  stream
       is  referred  to as "standard output"; and the error stream is referred
       to as "standard error".  These terms are abbreviated to form  the  sym‐
       bols used to refer to these files, namely stdin, stdout, and stderr.

所以基本上当你键入 wc 时它正在等待输入,例如你可以键入 bla 然后按 Ctrl+d 并且 wc 将终止并打印统计信息:

wc
bla
      1       1       4

与以下结果相同:

echo 'bla' | wc 
      1       1       4

在这种情况下,echo 命令的标准输出被发送到 wc

的标准输入

来自 man 7 管道:

       pipe()  creates  a pipe, a unidirectional data channel that can be used
       for interprocess communication. Data  written  to  the write end of the pipe is buffered by the
       kernel until it is read from the read end of the pipe.