有些程序根據它們的stdout是否爲tty來改變其輸出。所以如果你把它們放在一個管道中,或者重定向它們,輸出和你的shell中的輸出是不同的。這裏有一個例子:如何使stdin成爲tty?
$ touch a b c
# when running ls alone, it places them on one line
$ ls
a b c
# when stdout is not a tty, it places them on their own line
$ ls > output
$ cat output
a
b
c
output
所以,如果我想表明一個像這樣的命令應該是什麼樣子(例如我寫的教程),我要強調,輸出從終端複製然後將其保存到文件中。
好像我應該能夠做這樣的事情:
ls | ttypipe > output
凡ttypipe
是一個假設性的程序,其標準輸入(因此LS的stdout)響應與真當記者問,如果它是一個tty。
我知道,在Ruby中,我可以做這樣的事情:
require 'pty' # => true
IO.pipe.map(&:tty?) # => [false, false]
PTY.open.map(&:tty?) # => [true, true]
但是,這是一個孩子的過程,而不是當前的進程,所以結果:
$stdin.tty? # => false
我可以執行,我不能想辦法影響stdin文件描述符。
'$'stdin.tty使用IO(需要什麼, #tty?)返回'true'。 – steenslag 2015-02-06 19:05:04