为什么更多的命令不能读取stdin而是从管道stdin读取?

时间:2014-01-30 05:23:07

标签: linux unix command-line pipe

我怀疑more命令。通常,more无法从stdin读取,但使用管道从stdin读取内容。

例如,在尝试执行more命令以从stdin获取输入时,它正在拒绝。

$ more [Enter]
Usage: more [options] file...
Options:
-d        display help instead of ring bell
-f        count logical, rather than screen lines
-l        suppress pause after form feed
-p        suppress scroll, clean screen and disblay text
-c        suppress scroll, display text and clean line ends
-u        suppress underlining
-s        squeeze multiple blank lines into one
-NUM      specify the number of lines per screenful
+NUM      display file beginning from line number NUM
+/STRING  display file beginning from search string match
-V        output version information and exit

但是,这里是从管道stdin获取输入。

$ cat file.txt  
This is for testing purpose 
$ cat file.txt | more 
This is for testing purpose 

我想知道这是怎么回事(意思是不是从stdin阅读而是从管道stdin阅读?

1 个答案:

答案 0 :(得分:4)

more区分其标准输入是来自TTY还是来自其他地方(管道,常规文件等)。如注释中所述,如果输入来自TTY,more拒绝运行,因为它需要TTY来读取其命令键击。另一方面,cat不是交互式的,并且没有明确地处理TTY,所以它可以不关心它的输入是TTY还是其他类型的打开文件。

根据标准输入或输出是否为TTY,还有许多其他Unix实用程序的行为方式不同。例如,ls将其输出格式化为多列,而ls | cat则不会。