简体   繁体   English

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

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

Regarding 'wc' (word count) command... I am trying to understand the following (from 'man wc' which I have quoted below) "With no FILE, or when FILE is -, read standard input."关于'wc'(字数统计)命令......我试图理解以下内容(来自我在下面引用的'man wc') "With no FILE, or when FILE is -, read standard input."

How exactly does this work?这是如何工作的? At what point do I type the "standard input"?我在什么时候输入“标准输入”?

Finding explanations online rather confusing.网上找解释比较混乱。 Maybe I am just missing some basic info regarding what exactly stdin is.也许我只是缺少一些关于标准输入到底是什么的基本信息。

From 'man wc'来自 'man wc'

SYNOPSIS wc [OPTION]... [FILE]... wc [OPTION]... --files0-from=F概要 wc [选项]... [文件]... wc [选项]... --files0-from=F

DESCRIPTION Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified.描述 打印每个 FILE 的换行符、字和字节计数,如果指定了多个 FILE,则打印总行。 A word is a non-zero-length sequence of characters delimited by white space.单词是由空格分隔的非零长度字符序列。

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

Quoting man 3 stdin:引用 man 3 stdin:

       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.

so basically when you type wc it is waiting for input, for example you can type bla followed by Ctrl+d and wc will terminate and print the stats:所以基本上当你输入wc它正在等待输入,例如你可以输入 bla 后跟 Ctrl+d 并且 wc 将终止并打印统计信息:

wc
bla
      1       1       4

same result as:结果相同:

echo 'bla' | wc 
      1       1       4

in this case stdout of echo command is sent to stdin of wc在这种情况下, echo命令的标准输出被发送到wc标准输入

from man 7 pipe:来自 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM