Linux默認(rèn)提供三個(gè)特殊設(shè)備,用于終端顯示和輸出,分別為stdin
(標(biāo)準(zhǔn)輸入,對(duì)應(yīng)于終端的輸入),stdout
(標(biāo)準(zhǔn)輸出,對(duì)應(yīng)于終端的輸出),stderr
(標(biāo)準(zhǔn)錯(cuò)誤輸出,對(duì)應(yīng)于終端輸出)。
文件描述符 | 設(shè)備文件 | 說明 |
---|---|---|
0 |
/dev/stdin |
標(biāo)準(zhǔn)輸入 |
1 |
/dev/stdout |
標(biāo)準(zhǔn)輸出 |
2 |
/dev/stderr |
標(biāo)準(zhǔn)錯(cuò)誤 |
文件描述符:文件描述符形式上時(shí)一個(gè)非負(fù)整數(shù),實(shí)際上,它是一個(gè)索引值,指向內(nèi)核為每一個(gè)進(jìn)程所維護(hù)的該進(jìn)程打開文件的記錄表。當(dāng)程序打開一個(gè)現(xiàn)有文件或者創(chuàng)建一個(gè)新文件時(shí),內(nèi)核向進(jìn)程返回一個(gè)文件描述符。在程序設(shè)計(jì)中,一些涉及底層的程序編寫往往會(huì)圍繞著文件描述符展開。但是文件描述符這一概念只適用于UNIX、Linux這樣的操作系統(tǒng)。
一、標(biāo)準(zhǔn)重定向
- 將
cat
的連續(xù)輸出(heredoc方式)重定向到一個(gè)文件:
shiyanlou:~/ $ cat > eldon.c << EOF
heredoc> int mian()
heredoc> {
heredoc> return 0;
heredoc> }
heredoc> EOF
- 將
echo
命令的輸出從默認(rèn)的標(biāo)準(zhǔn)輸出重定向到一個(gè)文件:
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ū)別:管道默認(rèn)是連接前一個(gè)命令的輸出到下一個(gè)命令的輸入,而重定向通常是需要一個(gè)文件來建立兩個(gè)命令的連接。
二、標(biāo)準(zhǔn)錯(cuò)誤重定向:
標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤都被指向偽終端的屏幕顯示,所以我們經(jīng)??吹降囊粋€(gè)命令的輸出通常是同時(shí)包含了標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤:
shiyanlou:~/ $ cat eldon.c eldon.log
int mian()
{
return 0;
} #標(biāo)準(zhǔn)輸出
cat: eldon.log: No such file or directory #標(biāo)準(zhǔn)錯(cuò)誤
標(biāo)準(zhǔn)錯(cuò)誤雖然和標(biāo)準(zhǔn)輸出一樣都默認(rèn)指向偽終端屏幕,實(shí)際上他們的原理并不一樣,讀者可以自行領(lǐng)會(huì)下面操作的差異:
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
注意:需要在標(biāo)準(zhǔn)錯(cuò)誤重定向文件描述符前加上&
,否則shell會(huì)當(dāng)做重定向到一個(gè)文件名為1的文件中。
三、tee命令
可以使用tee
命令把輸出重定向到多個(gè)文件。
shiyanlou:~/ $ echo "eldonzhao" | tee eldon1 eldon2 eldon3
eldonzhao
shiyanlou:~/ $ ls
Code Desktop eldon1 eldon2 eldon3
四、永久重定向
我們可以通過exec
命令實(shí)現(xiàn)“永久”重定向。exec
命令的作用是使用指定的命令替換當(dāng)前的Shell,及使用一個(gè)進(jìn)程替換當(dāng)前進(jìn)程,或者指定新的重定向。
shiyanlou:~/ $ zsh #創(chuàng)建一個(gè)子Shell
shiyanlou:~/ $ exec 1>eldon.log
shiyanlou:~/ $ ls
shiyanlou:~/ $ exit #退出子Shell
shiyanlou:~/ $ cat eldon.log
Code
Desktop
eldon.log
五、創(chuàng)建輸出文件描述符
默認(rèn)在Shell中可以打開9個(gè)文件描述符,系統(tǒng)默認(rèn)只打開0
、1
、2
三個(gè)文件描述符,我們可以手動(dòng)打開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中有一個(gè)被稱為“黑洞”的設(shè)備文件,所以導(dǎo)入它的數(shù)據(jù)都將被“吞噬”。
在類UNIX系統(tǒng)中,/dev/null或稱為空設(shè)備,是一個(gè)特殊的設(shè)備文件,它通常被用于丟棄不需要的輸出流,或作為用于輸入流的空文件,這些操作通常由重定向完成。讀取它則會(huì)得到一個(gè)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ù)列表過長(zhǎng)。
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