Linux I/O重定向
Unix 命令行的功能之一是使用輸入/輸出重定向和管道。在本次會(huì)議中,我們介紹了輸入、輸出和錯(cuò)誤流的重定向。
Linux I/O重定向
標(biāo)準(zhǔn)輸入、標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤
bash shell 具有三個(gè)基本流;它從 stdin(流 0)獲取輸入,將輸出發(fā)送到 stdout(流 1),并將錯(cuò)誤消息發(fā)送到 stderr(流 2)。
鍵盤通常用作標(biāo)準(zhǔn)輸入,而標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤都連接到顯示器。這可能會(huì)讓 Linux 新用戶感到困惑,因?yàn)闆]有明顯的方法來識別 stdout 和 stderr。有經(jīng)驗(yàn)的用戶知道將輸出與錯(cuò)誤分開非常有用。
下一節(jié)將解釋如何重定向這些流。
輸出重定向
標(biāo)準(zhǔn)輸出 (>)
stdout 可以使用大于號重定向。掃描該行時(shí),shell 將看到 > 符號并清除文件。
事實(shí)上,> 符號是 1> 的縮寫(stdout 被稱為流 1)。
datasoft @ datasoft-linux ~/test10$ echo It is summer today!
It is summer today!
datasoft @ datasoft-linux ~/test10$ echo It is summer today! > summer.txt
datasoft @ datasoft-linux ~/test10$ cat summer.txt
It is summer today!
datasoft @ datasoft-linux ~/test10$
請注意,bash shell 在執(zhí)行參數(shù) 0 之前有效地從命令行刪除了重定向。這意味著在此命令的情況下:
datasoft @ datasoft-linux ~/test10$ echo Hei > greeting.txt
shell 只計(jì)算兩個(gè)參數(shù)(echo = 參數(shù) 0,hello = 參數(shù) 1)。在參數(shù)計(jì)數(shù)發(fā)生之前重定向被刪除。
輸出文件被刪除
掃描該行時(shí),shell 將看到 > 符號并清除文件!由于這發(fā)生在解析參數(shù) 0 之前,這意味著即使命令失敗,文件也將被清除!
datasoft @ datasoft-linux ~/test10$ cat summer.txt
It is summer today!
datasoft @ datasoft-linux ~/test10$ zcho It is summer today! > summer.txt
No command 'zcho' found, did you mean:
Command 'echo' from package 'coreutils' (main)
zcho: command not found
datasoft @ datasoft-linux ~/test10$ cat summer.txt
datasoft @ datasoft-linux ~/test10$
noclobber
過設(shè)置 noclobber 選項(xiàng)可以防止使用 > 時(shí)擦除文件。
datasoft @ datasoft-linux ~/test10$ cat summer.txt
datasoft @ datasoft-linux ~/test10$ set -o noclobber
datasoft @ datasoft-linux ~/test10$ echo It is cold today! > summer.txt
bash: summer.txt: cannot overwrite existing file
datasoft @ datasoft-linux ~/test10$ set +o noclobber
datasoft @ datasoft-linux ~/test10$
否決noclobber
noclobber 可以用 >| 來否決。
datasoft @ datasoft-linux ~/test10$ set -o noclobber
datasoft @ datasoft-linux ~/test10$ echo It is summer today! > summer.txt
bash: summer.txt: cannot overwrite existing file
datasoft @ datasoft-linux ~/test10$ echo It is summer today! >|summer.txt
datasoft @ datasoft-linux ~/test10$ cat summer.txt
It is summer today!
datasoft @ datasoft-linux ~/test10$
追加 (>>)
使用 >> 將輸出附加到文件。
datasoft @ datasoft-linux ~/test10$ echo It is summer today! > summer.txt
bash: summer.txt: cannot overwrite existing file
datasoft @ datasoft-linux ~/test10$ cat summer.txt
It is summer today!
datasoft @ datasoft-linux ~/test10$ echo Where is the hot summer ? >> summer.txt
datasoft @ datasoft-linux ~/test10$ cat summer.txt
It is summer today!
Where is the hot summer ?
datasoft @ datasoft-linux ~/test10$
錯(cuò)誤重定向
2> 標(biāo)準(zhǔn)錯(cuò)誤
重定向 stderr 是通過 2> 完成的。這對于防止屏幕上出現(xiàn)錯(cuò)誤消息非常有用。
下面的屏幕截圖顯示了 stdout 重定向到文件,stderr 重定向到 /dev/null。寫 1> 與 > 相同。
datasoft @ datasoft-linux ~/test10$ find / > allfiles.txt 2> /dev/null
datasoft @ datasoft-linux ~/test10$
2>&1
要將 stdout 和 stderr 重定向到同一文件,請使用 2>&1。
datasoft @ datasoft-linux ~/test10$ find / > allfiles_and_error.txt 2>&1
請注意,重定向的順序很重要。例如,命令
ls > dirlist 2>&1
將標(biāo)準(zhǔn)輸出(文件描述符 1)和標(biāo)準(zhǔn)錯(cuò)誤(文件描述符 2)定向到文件目錄列表,而命令
僅將標(biāo)準(zhǔn)輸出定向到文件 dirlist,因?yàn)闃?biāo)準(zhǔn)錯(cuò)誤在標(biāo)準(zhǔn)輸出重定向到 dirlist 之前復(fù)制了標(biāo)準(zhǔn)輸出。
輸出重定向和管道
默認(rèn)情況下,在命令行上使用管道時(shí),無法在 stderr 內(nèi)進(jìn)行 grep,因?yàn)橹粋鬟f了 stdout。
datasoft @ datasoft-linux ~/test10$ rm file35 file=10 | grep test
rm: cannot remove 'file35': No such file or directory
rm: cannot remove 'file=10': No such file or directory
datasoft @ datasoft-linux ~/test10$ rm file10 | grep.txt
使用 2>&1 您可以強(qiáng)制 stderr 轉(zhuǎn)到 stdout。這使得管道中的下一個(gè)命令能夠作用于兩個(gè)流。
datasoft @ datasoft-linux ~/test10$ rm file35 file10 file101 2>&1 1>&2 | grep file35
rm: cannot remove 'file35': No such file or directory
datasoft @ datasoft-linux ~/test10$
您不能同時(shí)使用 1>&2 和 2>&1 來切換 stdout 和 stderr。
datasoft @ datasoft-linux /$ rm file35 file10 file101 2>&1 1>&2 | grep file35
rm: cannot remove 'file35': No such file or directory
datasoft @ datasoft-linux /$ echo file35 2>&1 1>&2 | sed 's/file35/FILE35/'
FILE35
您需要第三個(gè)流來在管道符號之后切換 stdout 和 stderr。
datasoft @ datasoft-linux /$ echo file35 2>&1 1>&2 | sed 's/file35/FILE35/'
FILE35
datasoft @ datasoft-linux /$ echo file35 3>&1 1>&2 2>&3 | sed 's/file35/FILE35/' file35
file35
sed: can't read file35: No such file or directory
datasoft @ datasoft-linux /$
連接 stdout 和 stderr
&> 構(gòu)造會(huì)將 stdout 和 stderr 放入一個(gè)流中(到一個(gè)文件)。
datasoft @ datasoft-linux ~$ cd test10
datasoft @ datasoft-linux ~/test10$ rm file35 &> out_and_err
datasoft @ datasoft-linux ~/test10$ cat out_and_err
rm: cannot remove 'file35': No such file or directory
datasoft @ datasoft-linux ~/test10$ echo file35 &> out_and_err
bash: out_and_err: cannot overwrite existing file
datasoft @ datasoft-linux ~/test10$
輸入重定向
標(biāo)準(zhǔn)輸入(<)
重定向標(biāo)準(zhǔn)輸入是通過 < (0< 的簡寫)完成的。
datasoft @ datasoft-linux ~$ cat < mno.txt
What is your name?
My name is prasanta.
datasoft @ datasoft-linux ~$ tr 'onetw' 'onezz' < mno.txt
Whaz is your name?
My name is prasanza.
datasoft @ datasoft-linux ~$
here-is-document是一種追加輸入直到遇到特定序列(通常是EOF)的方法。 EOF 標(biāo)記可以按字面輸入,也可以使用 Ctrl-D 調(diào)用。
檢查下面的代碼
datasoft @ datasoft-linux ~$ cat < mno.txt
> What is your name?
> My name is prasanta.
> EOF
bash: mno.txt: cannot overwrite existing file
datasoft @ datasoft-linux ~$ cat mno.txt
What is your name?
My name is prasanta.
datasoft @ datasoft-linux ~$ cat < mno.txt
> What's your name?
> prasanta
> ^C
datasoft @ datasoft-linux ~$ cat mno.txt
What is your name?
My name is prasanta.
datasoft @ datasoft-linux ~$
版權(quán)所屬:SO JSON在線解析
原文地址:http://zijieyoumin.cn/blog/532.html
轉(zhuǎn)載時(shí)必須以鏈接形式注明原始出處及本聲明。
如果本文對你有幫助,那么請你贊助我,讓我更有激情的寫下去,幫助更多的人。