2010/03/25

Standard I/O Redirection

Linux 提供三種 I/O Streams 給程式使用

STDIN (0) : default is keyboard.

STDOUT (1) : default is terminal.
STDERR (2) : default is terminal.

而 STDOUT 和 STDERR 可以被重新導向至檔案 ( I/O Redirection ), 作法如下:

$ binary > (將 STDOUT 重新導向)
$ binary 2> (將 STDERR 重新導向)
$ binary &> (將所有輸出重新導向)
$ binary 2>&1 (將 STDERR 重新導向至 STDOUT)
$ binary >&2 (將STDOUT 重新導向至 STDERR)

使用 > 輸出導向的檔案預設會被完全覆蓋, 也可改用 >> 來添加在原檔案的結尾

而關閉 File Description 的方式則如下:

$ n<&- (關閉 Input File Description n)
$ 0<&- , $ <&- (關閉 STDIN)
$ m>&- (關閉 Output File Description m)
$ 1>&- , $ >&- (關閉 STDOUT)
reference : http://tldp.org/LDP/abs/html/io-redirection.html


沒有留言:

張貼留言