Linux默認提供三個特殊設(shè)備,用于終端顯示和輸出,分別為stdin(標準輸入,對應于終端的輸入),stdout(標準輸出,對應于終端的輸出),stderr(標準錯誤輸出,對應于終端輸出)。
| 文件描述符 | 設(shè)備文件 | 說明 |
|---|---|---|
0 |
/dev/stdin |
標準輸入 |
1 |
/dev/stdout |
標準輸出 |
2 |
/dev/stderr |
標準錯誤 |
文件描述符:文件描述符形式上時一個非負整數(shù),實際上,它是一個索引值,指向內(nèi)核為每一個進程所維護的該進程打開文件的記錄表。當程序打開一個現(xiàn)有文件或者創(chuàng)建一個新文件時,內(nèi)核向進程返回一個文件描述符。在程序設(shè)計中,一些涉及底層的程序編寫往往會圍繞著文件描述符展開。但是文件描述符這一概念只適用于UNIX、Linux這樣的操作系統(tǒng)。
一、標準重定向
- 將
cat的連續(xù)輸出(heredoc方式)重定向到一個文件:
shiyanlou:~/ $ cat > eldon.c << EOF
heredoc> int mian()
heredoc> {
heredoc> return 0;
heredoc> }
heredoc> EOF
- 將
echo命令的輸出從默認的標準輸出重定向到一個文件:
shiyanlou:~/ $ echo "hello world" > redirect
shiyanlou:~/ $ cat redirect
hello world
shiyanlou:~/ $ echo "hello world1" >> redirect
shiyanlou:~/ $ cat redirect
hello world
hello world1
shiyanlou:~/ $ echo "hello world" > redirect
shiyanlou:~/ $ cat redirect
hello world
注意管道和重定向的區(qū)別:管道默認是連接前一個命令的輸出到下一個命令的輸入,而重定向通常是需要一個文件來建立兩個命令的連接。
二、標準錯誤重定向:
標準輸出和標準錯誤都被指向偽終端的屏幕顯示,所以我們經(jīng)??吹降囊粋€命令的輸出通常是同時包含了標準輸出和標準錯誤:
shiyanlou:~/ $ cat eldon.c eldon.log
int mian()
{
return 0;
} #標準輸出
cat: eldon.log: No such file or directory #標準錯誤
標準錯誤雖然和標準輸出一樣都默認指向偽終端屏幕,實際上他們的原理并不一樣,讀者可以自行領(lǐng)會下面操作的差異:
shiyanlou:~/ $ cat eldon.log > errlog
cat: eldon.log: No such file or directory
shiyanlou:~/ $ cat eldon.log &> errlog
shiyanlou:~/ $ cat errlog
cat: eldon.log: No such file or directory
shiyanlou:~/ $ cat eldon.log > mylog 2>&1
shiyanlou:~/ $ cat mylog\
cat: eldon.log: No such file or directory
注意:需要在標準錯誤重定向文件描述符前加上&,否則shell會當做重定向到一個文件名為1的文件中。
三、tee命令
可以使用tee命令把輸出重定向到多個文件。
shiyanlou:~/ $ echo "eldonzhao" | tee eldon1 eldon2 eldon3
eldonzhao
shiyanlou:~/ $ ls
Code Desktop eldon1 eldon2 eldon3
四、永久重定向
我們可以通過exec命令實現(xiàn)“永久”重定向。exec命令的作用是使用指定的命令替換當前的Shell,及使用一個進程替換當前進程,或者指定新的重定向。
shiyanlou:~/ $ zsh #創(chuàng)建一個子Shell
shiyanlou:~/ $ exec 1>eldon.log
shiyanlou:~/ $ ls
shiyanlou:~/ $ exit #退出子Shell
shiyanlou:~/ $ cat eldon.log
Code
Desktop
eldon.log
五、創(chuàng)建輸出文件描述符
默認在Shell中可以打開9個文件描述符,系統(tǒng)默認只打開0、1、2三個文件描述符,我們可以手動打開3-8的文件描述符。
shiyanlou:~/ $ exec 3>eldon.err[11:13:16]
shiyanlou:~/ $ cd /dev/fd;ls -Al; cd -[11:13:18]
total 0
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:12 0 -> /dev/pts/0
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:13 1 -> /dev/pts/0
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:13 10 -> /dev/pts/0
lr-x------ 1 shiyanlou shiyanlou 64 Jan 11 11:13 12 -> /usr/share/zsh/functio
ns/Completion.zwc
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:13 2 -> /dev/pts/0
l-wx------ 1 shiyanlou shiyanlou 64 Jan 11 11:13 3 -> /home/shiyanlou/eldon.e
rr
~
shiyanlou:~/ $ echo "this is eldon" >&3
shiyanlou:~/ $ cat eldon.err
this is eldon
shiyanlou:~/ $ exit
六、關(guān)閉文件描述符
shiyanlou:~/ $ exec 3>eldon.err
shiyanlou:~/ $ cd /dev/fd;ls -Al;cd -
total 0
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:17 0 -> /dev/pts/0
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:17 1 -> /dev/pts/0
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:19 10 -> /dev/pts/0
lr-x------ 1 shiyanlou shiyanlou 64 Jan 11 11:19 12 -> /usr/share/zsh/functio
ns/Completion.zwc
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:17 2 -> /dev/pts/0
l-wx------ 1 shiyanlou shiyanlou 64 Jan 11 11:17 3 -> /home/shiyanlou/eldon.e
rr
~
shiyanlou:~/ $ exec 3>&-
shiyanlou:~/ $ cd /dev/fd;ls -Al;cd -
total 0
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:17 0 -> /dev/pts/0
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:17 1 -> /dev/pts/0
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:19 10 -> /dev/pts/0
lr-x------ 1 shiyanlou shiyanlou 64 Jan 11 11:19 12 -> /usr/share/zsh/functio
ns/Completion.zwc
lrwx------ 1 shiyanlou shiyanlou 64 Jan 11 11:17 2 -> /dev/pts/0
~
七、完全屏蔽命令輸出
在Linux中有一個被稱為“黑洞”的設(shè)備文件,所以導入它的數(shù)據(jù)都將被“吞噬”。
在類UNIX系統(tǒng)中,/dev/null或稱為空設(shè)備,是一個特殊的設(shè)備文件,它通常被用于丟棄不需要的輸出流,或作為用于輸入流的空文件,這些操作通常由重定向完成。讀取它則會得到一個EOF。
shiyanlou:~/ $ cat eldon.log
cat: eldon.log: No such file or directory
shiyanlou:~/ $ cat eldon.log 1>/dev/null 2>&1
八、使用xargs分割參數(shù)列表
xargs是一條UNIX和類UNIX操作系統(tǒng)的常用命令。它的作用是將參數(shù)列表轉(zhuǎn)換成小塊分段傳遞給其他命令,以避免參數(shù)列表過長。
shiyanlou:~/ $ cut -d : -f 1 < /etc/passwd | sort| xargs echo
backup bin daemon games gnats irc libuuid list lp mail man memcache messagebu
s mongodb mysql news nobody proxy redis root shiyanlou sshd sync sys syslog t
omcat7 uucp www-data