八、I/O重定向與管道

系統設定:

  標準輸入 standard input(stdin):代碼爲0 ,使用< 或<<

  標準輸出 standard output(stdout):代碼爲1,使用>或>>

  標準錯誤輸出 standard error output(stderr):代碼爲2,使用 2>或2>>


標準輸入<  <<:

< :就是將原本需要由鍵盤輸入的數據,改由文本內容來取代,輸入重定向

<< :結束的輸入字符


標準輸出 > >> :

[root@www ~]# ls -l >rootfile

[root@www ~]# cat rootfile

總用量 68

-rw-------. 1 root root  1454  8月  7 00:09 anaconda-ks.cfg

-rw-r--r--. 1 root root 45182  8月  7 00:09 install.log

-rw-r--r--. 1 root root  9922  8月  7 00:07 install.log.syslog

-rw-r--r--. 1 root root     0 10月 31 21:26 rootfile

-rw-r--r--. 1 root root     0 10月 11 20:08 text.txt

在輸出重定向時,如果重定向的文本部存在(如 rootfile)那麼系統會自動創建,但如果這個文本存在且有數據,那麼系統會自動將這個文本清空,然後再將數據寫入。

使用[>]會將文本原來的數據覆蓋,使用[>>]不會覆蓋數據,只是累加。


set -C: if set ,disallow existing regular files to be overwritten by redirection of output.(禁止對已存在的文件使用覆蓋重定向)

     此時要強制覆蓋輸出的話,則使用 >|

set +C:關閉上述功能


標準錯誤輸出 2> 2>> :

2> :以覆蓋的方法將[錯誤的數據]輸出到指定的文本上

2>> :以累加的方法將[錯誤的數據]輸出到指定的文本上

[root@www ~]# cat text.txt text1 >stdout 2>stderr

[root@www ~]# cat stdout 

hello,

kkkkkkk

[root@www ~]# cat stderr 

cat: text1: 沒有那個文件或目錄

可以將stdout與stderr重定向到不同的文本上

[root@www ~]# cat text.txt text &>std

[root@www ~]# cat std

hello,

kkkkkkk

cat: text: 沒有那個文件或目錄

可以使用&> 將標準輸出與標準錯誤輸出重定向到同一個文本上


tee:read from standard input and write to standard output and files(可以將輸出數據顯示到屏幕和文本中)

tee[option]...[file]...

-a,append:append to given FILEs,do not overwrite(以累加的方式將數據加入file中)

[root@www ~]# ls |tee 1

anaconda-ks.cfg

install.log

install.log.syslog

[root@www ~]# cat 1

anaconda-ks.cfg

install.log

install.log.syslog


管道:前一個命令的輸出,作爲後一個命令的輸入

命令1|命令2|命令3|。。。。。



地址總線:內存尋址

數據總線:傳輸數據

控制總線:控制指令

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章