linux中的重定向和管道的使用方法

一個程序運行就必須要有指令和數據或者說數據結構和算法。程序處理的數據來源和處理後存放在哪,是程序員必須要考慮額問題。每個程序都有讀入數據和輸出數據的需求,但是爲了便捷,程序允許缺省輸入和輸出,也就是使用默認的輸入輸出。一般稱之爲標準輸入和標準輸出。

對於用戶來說,訪問文件是通過文件名來進行的,但對於內核來說則是一個非零整數,這個數字叫做文件描述符(file descriptor,fd),打開已存在的文件或新建一個文件時,內核會返回一個文件描述符,讀寫文件也需要使用文件描述符來指定帶讀寫的文件。

注意:文件描述符和inode號是有區別的,inode號是文件系統創建時就已經確定的,當創建一個新的文件或目錄時,系統從inode表中選出一個空閒的inode號來唯一標識這個文件,並將文件的數據塊位置存在inode裏,當許多用戶對同一個文件進行操作時,內核將爲每位用戶分配一個文件描述符,即一個文件可能對應多個文件描述符。

具體的Linux文件系統原理可以參考:

https://www.ibm.com/developerworks/cn/linux/l-linux-filesystem/

https://akaedu.github.io/book/ch29s03.html

https://www.ibm.com/developerworks/cn/linux/l-vfs/  

標準輸入的文件描述符是0,標準輸出是1,標準錯誤輸出是2

I/O重定向:改變標準位置

重定向支持的操作符有三種:

>:標準輸出重定向,可以輸出到指定的文件,但會覆蓋文件內容,>>則是在文件內容的後面追加新內容

2>:重定向錯誤到文件,和>類似,2>>表示追加

<:輸出重定向

set -C:禁止重定向到文件時覆蓋已存在的文件,只對當前shell有效

set +C:撤銷禁用覆蓋

[root@localhost testdir]# ajf 2> afi
[root@localhost testdir]# set -C
[root@localhost testdir]# ajf 2> afi
-bash: afi: cannot overwrite existing file
[root@localhost testdir]# ajf 2> a
[root@localhost testdir]# ajf 2> a
-bash: a: cannot overwrite existing file

可以將標準輸出和標準錯誤輸出分別重定向到不同文件

[root@localhost testdir]# ll
total 8
-rw-r--r--. 1 root root 32 Jul 31 09:50 
a-rw-r--r--. 1 root root 32 Jul 31 09:48 afi
[root@localhost testdir]# ll a afi mage > f1 2> f2
[root@localhost testdir]# cat f1 
-rw-r--r--. 1 root root 32 Jul 31 09:50 a
-rw-r--r--. 1 root root 32 Jul 31 09:48 afi
[root@localhost testdir]# cat f2
ls: cannot access mage: No such file or directory

如果要將輸出和錯誤同時寫入一個文件,也可以用更簡便的寫法

[root@localhost testdir]# cat f1 f2 f3 &> f4
[root@localhost testdir]# cat f4
-rw-r--r--. 1 root root 32 Jul 31 09:50 a
-rw-r--r--. 1 root root 32 Jul 31 09:48 afi
ls: cannot access mage: No such file or directory
cat: f3: No such file or directory

或者用下面的老寫法

[root@localhost testdir]# cat f1 f2 f3 > f5 2>&1
[root@localhost testdir]# cat f5
-rw-r--r--. 1 root root 32 Jul 31 09:50 a
-rw-r--r--. 1 root root 32 Jul 31 09:48 afi
ls: cannot access mage: No such file or directory
cat: f3: No such file or directory

同理,&>>表示追加

[root@localhost testdir]# cat f1 f2 f3 >> f5 2>&1
[root@localhost testdir]# cat f1 f2 f3 &>> f4

也可以合併多個命令的輸出

[root@localhost testdir]# (cat f4;ls ) > f3
[root@localhost testdir]# cat f3
-rw-r--r--. 1 root root 32 Jul 31 09:50 a
-rw-r--r--. 1 root root 32 Jul 31 09:48 afi
ls: cannot access mage: No such file or directory
cat: f3: No such file or directory
-rw-r--r--. 1 root root 32 Jul 31 09:50 a
-rw-r--r--. 1 root root 32 Jul 31 09:48 afi
ls: cannot access mage: No such file or directory
cat: f3: No such file or directory
a
afi
f1
f2
f3
f4
f5

可以看到,先創建了重定向的文件,在執行前面的內容,然後將輸出寫入文件

[root@localhost testdir]# rm -rf *
[root@localhost testdir]# ll        #此時沒有文件
total 0
[root@localhost testdir]# ll > file1      #ll當前空目錄並重定向到file1
[root@localhost testdir]# cat file1      #然而file1的內容不爲空,顯示了它自己的信息,並且它的大小是0
total 0
-rw-r--r--. 1 root root 0 Jul 31 10:05 file1

輸入重定向:<

使用<可以配合cat創建並寫入文件

[root@localhost testdir]# cat <<eof
> aaa
> bbb
> ccc
> 
> eof
aaa
bbb
ccc

必須是<<,eof爲定義的結束標誌,可以是任意字符,也可以將輸出重定向到文件

[root@localhost testdir]# cat >> aaa <<eof
> welcome to the new world!
> eof
[root@localhost testdir]# cat aaa
welcome to the new world!

tr命令:轉換或輸出字符串

語法:tr [OPTIONS] set1 [set2]

tr複製標準輸入到標準輸出,並且執行轉化、擠壓重複字符串、刪除字符串、先刪除然後將輸出再擠壓重複的字符

-c,-C,--complement 取字符集的子集

-d,--delete 刪除屬於第一字符集的字符

-s,--squeeze-repeats 把連續且重複的字符用一個表示

-t,--truncate-set1 將第一個字符集對應的字符轉化爲第二個字符集對應的字符

 mail 發送那個信息:

mail -s NAME WHO

管道:

將命令1的標準輸出發送給命令2的標準輸入,以此類推

標準錯誤默認不能通過管道傳輸,可用2>&1或|&實現

最後一個命令會在當前shell進程的子shell進程中執行

重定向到多個目標:tee

[root@localhost testdir]# ls |tee f1 
aaa
asd
file1
[root@localhost testdir]# cat f1
aaa
asd
file1

tee會將命令的輸出重定向到標準輸出和一個文件




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