tr围详,tee
程序的IO
簡(jiǎn)單的說(shuō)程序由指令(命令)和數(shù)據(jù)(操作對(duì)象)組成襟铭,在linux上一切皆文件,程序操作對(duì)象(數(shù)據(jù))來(lái)自系統(tǒng)上的各種文件即程序的輸入短曾,由程序處理之后輸出的結(jié)果即程序的輸出
- 數(shù)據(jù)的輸入與輸出
- 可用于輸入的設(shè)備:文件寒砖,鍵盤(pán)設(shè)備、文件系統(tǒng)上的常見(jiàn)文件嫉拐、網(wǎng)卡等
- 可用于輸出的設(shè)備:文件哩都,顯示器、文件系統(tǒng)上常見(jiàn)的文件婉徘、網(wǎng)卡等
- 程序數(shù)據(jù)流的三種狀態(tài)或形式
- 輸入的數(shù)據(jù)流:標(biāo)準(zhǔn)輸入(stdin)漠嵌,默認(rèn)為從鍵盤(pán)
- 輸出的數(shù)據(jù)流:標(biāo)準(zhǔn)輸出(stdout),默認(rèn)輸出到顯示器
命令執(zhí)行所回傳的正確的信息 - 錯(cuò)誤輸出數(shù)據(jù)流:錯(cuò)誤輸出(stderr)盖呼,默認(rèn)輸出到顯示器
命令執(zhí)行失敗后儒鹿,所回傳的錯(cuò)誤信息
- fd:file descriptor,文件描述符
簡(jiǎn)單說(shuō)几晤,它是一個(gè)索引值约炎,指向 內(nèi)核為每一個(gè)進(jìn)程所維護(hù)的該進(jìn)程打開(kāi)文件的記錄表;
本人目前的理解只有這么多,后續(xù)學(xué)習(xí)中在進(jìn)行補(bǔ)充文件描述符的相關(guān)知識(shí)圾浅。
IO重定向會(huì)圍繞描述符來(lái)展開(kāi)掠手,列舉以下三種文件描述符- 標(biāo)準(zhǔn)輸入:0
- 標(biāo)準(zhǔn)輸出:1
- 錯(cuò)誤輸出:2
IO重定向
不使用系統(tǒng)默認(rèn)的標(biāo)準(zhǔn)輸入輸出,而是重新指定,即輸入重定向,輸出重定向,錯(cuò)誤輸出重定向,雙重輸出重定向(一次單獨(dú)地送到不同的地方)。IO重定向其實(shí)就是讓已創(chuàng)建的FD指向其它的文件(修改其鏈接的文件)狸捕。輸入輸出數(shù)據(jù)流的位置用FD標(biāo)識(shí)
輸出重定向
- 標(biāo)準(zhǔn)輸出重定向
喷鸽,即1>
特性:覆蓋輸出
* >>,即1>>
特性:追加輸出
* # set -C
禁止覆蓋輸出重定向至已存在的文件灸拍,只對(duì)當(dāng)前shell進(jìn)程生效
此時(shí)可使用強(qiáng)制覆蓋輸出:>|
* # set +C
關(guān)閉上述特性
- 錯(cuò)誤輸出重定向
- 2>
特性:覆蓋追加輸出 - 2>>
特性:追加錯(cuò)誤輸出
- 2>
- 合并標(biāo)準(zhǔn)輸出流和錯(cuò)誤輸出流
- &>做祝,覆蓋合并
- &>>,覆蓋追加
- COMMAND > /path/to/somefile 2>&1鸡岗,前面>是覆蓋后面>就是覆蓋
- COMMAND >> /path/to/somefile 2>&1混槐,前面>>是追加后邊的>就是追加
- 特殊設(shè)備:/dev/null
將輸出的數(shù)據(jù)丟到此目錄,所有的數(shù)據(jù)都會(huì)無(wú)聲無(wú)息被丟棄纤房。根據(jù)命令的執(zhí)行結(jié)果可以判斷目錄及文件的存在與否及內(nèi)容有無(wú)
輸入重定向
<纵隔,會(huì)經(jīng)常和tr命令一起使用。介紹下tr命令的用法
- image.png
-
Here Document :<<炮姨,此處創(chuàng)建文檔
- cat << EOF
- cat > /PATH/TO/SOMEFILE << EOF
管道
鏈接程序捌刮,實(shí)現(xiàn)前一個(gè)命令的輸出直接定向到后一個(gè)程序并當(dāng)作輸入數(shù)據(jù)流(Linux的哲學(xué)思想)
只會(huì)將最后一個(gè)命令的執(zhí)行結(jié)果,作為標(biāo)準(zhǔn)輸出舒岸,前面的命令的輸出都會(huì)作為下一個(gè)命令的輸入绅作,不會(huì)在屏幕上顯示了。
COMMAND1 | COMMAND2 | COMMAND3 |……
管道符與tee命令結(jié)合使用蛾派,將前一個(gè)命令的標(biāo)準(zhǔn)輸出保存一份俄认,然后繼續(xù)通過(guò)管道符送到下一個(gè)命令,實(shí)現(xiàn)命令分方向發(fā)送洪乍。tee介紹如下:
- image.png
相關(guān)演示
[root@localhost mytest]# touch fedora \\創(chuàng)建一個(gè)空文件
[root@localhost mytest]# ll
總用量 0
-rw-r--r--. 1 root root 0 8月 13 22:50 fedora
[root@localhost mytest]# echo diyihang > fedora \\將echo回顯的內(nèi)容重定向到fedora
[root@localhost mytest]# cat fedora
diyihang
[root@localhost mytest]# echo dierhang > fedora \\覆蓋重定向眯杏,第一次的內(nèi)容被覆蓋
[root@localhost mytest]# cat fedora
dierhang
[root@localhost mytest]# echo desanhang >> fedora \\追加重定向
[root@localhost mytest]# cat fedora
dierhang
desanhang
[root@localhost mytest]# set -C \\禁止重定向覆蓋已存在的文件,即使文件為空的壳澳。
[root@localhost mytest]# echo disihang > fedora
-bash: fedora: 無(wú)法覆蓋已存在的文件
[root@localhost mytest]# echo disihang >> fedora \\只可以追加
[root@localhost mytest]# cat fedora
dierhang
desanhang
disihang
[root@localhost mytest]# echo dierhang >| fedora \\強(qiáng)制覆蓋
[root@localhost mytest]# cat fedora
dierhang
[root@localhost mytest]# set +C \\取消進(jìn)制覆蓋重定向
[root@localhost mytest]# echo diyihang > fedora
[root@localhost mytest]# cat fedora
diyihang
[root@localhost mytest]# > fedora
[root@localhost mytest]# cat fedora
[root@localhost mytest]# echo dierhang > suse \\重定向的文件不存在岂贩,會(huì)自動(dòng)創(chuàng)建此文件
[root@localhost mytest]# ll
總用量 8
-rw-r--r--. 1 root root 9 8月 13 22:54 fedora
-rw-r--r--. 1 root root 9 8月 13 22:54 suse
[root@localhost mytest]# cat suse
dierhang
[root@localhost mytest]# > centos \\重定向到新文件,什么也沒(méi)有巷波,即創(chuàng)建空文件萎津,相當(dāng)于touch
[root@localhost mytest]# ll
總用量 8
-rw-r--r--. 1 root root 0 8月 13 22:54 centos
-rw-r--r--. 1 root root 9 8月 13 22:54 fedora
-rw-r--r--. 1 root root 9 8月 13 22:54 suse
[root@localhost mytest]# ll centos1
ls: 無(wú)法訪問(wèn)centos1: 沒(méi)有那個(gè)文件或目錄
[root@localhost mytest]# ll centos1 2> ubuntu
[root@localhost mytest]# cat ubuntu
ls: 無(wú)法訪問(wèn)centos1: 沒(méi)有那個(gè)文件或目錄
[root@localhost mytest]# ll centos2 2>> ubuntu
[root@localhost mytest]# cat ubuntu
ls: 無(wú)法訪問(wèn)centos1: 沒(méi)有那個(gè)文件或目錄
ls: 無(wú)法訪問(wèn)centos2: 沒(méi)有那個(gè)文件或目錄
[root@localhost mytest]# cat fedora
diyihang
[root@localhost mytest]# cat fedora &> suse \\合并重定向
[root@localhost mytest]# cat suse
diyihang
[root@localhost mytest]# cat fedora1 &>> suse
[root@localhost mytest]# cat suse
diyihang
cat: fedora1: 沒(méi)有那個(gè)文件或目錄
[root@localhost mytest]# cat fedora1
cat: fedora1: 沒(méi)有那個(gè)文件或目錄
[root@localhost mytest]# cat fedora1 > suse 2>&1
[root@localhost mytest]# cat suse
cat: fedora1: 沒(méi)有那個(gè)文件或目錄
[root@localhost mytest]# cat fedora > suse 2>&1
[root@localhost mytest]# cat suse
diyihang
[root@localhost mytest]# cat fedora1 >> suse 2>&1
[root@localhost mytest]# cat suse
diyihang
cat: fedora1: 沒(méi)有那個(gè)文件或目錄
[root@localhost mytest]# ls /tmp/mytest/
centos fedora suse ubuntu
[root@localhost mytest]# ls /tmp/mytest1 &> /dev/null \\可以通過(guò)合并重定向到null文件,根據(jù)命令執(zhí)行的狀態(tài)值抹镊,判斷是否有該目錄锉屈。
[root@localhost mytest]# echo $? \\返回值為2,命令執(zhí)行失敗垮耳,所以沒(méi)有mytest1目錄
2
[root@localhost mytest]# ls /tmp/mytest &> /dev/null
[root@localhost mytest]# echo $? \\返回值為0颈渊,命令執(zhí)行成功,所以有mytest目錄
0
[root@localhost mytest]# cat fedora
diyihang
dierhang
disanhang
disiheng
5diwuhang6
di6hang
[root@localhost mytest]# tr [a-z] [A-Z] < fedora \\通過(guò)符號(hào)"<"將文件的內(nèi)容送給命令tr處理,即將所有小寫(xiě)字母儡炼,變?yōu)榇髮?xiě)妓湘。"<"后邊為已經(jīng)存在的文件
DIYIHANG
DIERHANG
DISANHANG
DISIHENG
5DIWUHANG6
DI6HANG
[root@localhost mytest]# cat fedora
diyihang
dierhang
disanhang
disiheng
5diwuhang6
di6hang
[root@localhost mytest]# tr -d "hang" < fedora \\刪除fedora中出現(xiàn)的字符串hang查蓉,非單詞乌询。
diyi
dier
dis
disie
5diwu6
di6
[root@localhost mytest]# tr -d "[[:digit:]]" < fedora
diyihang
dierhang
disanhang
disiheng
diwuhang
dihang
[root@localhost mytest]# tr -d "[0-9]" < fedora
diyihang
dierhang
disanhang
disiheng
diwuhang
dihang
[root@localhost mytest]# ll
總用量 12
-rw-r--r--. 1 root root 0 8月 13 22:54 centos
-rw-r--r--. 1 root root 56 8月 13 23:33 fedora
-rw-r--r--. 1 root root 51 8月 13 23:26 suse
-rw-r--r--. 1 root root 106 8月 13 22:57 ubuntu
[root@localhost mytest]# ll | head -2 \\通過(guò)ll命令的輸出作為輸入交給head -2處理,后者處理后輸出到屏幕
總用量 12
-rw-r--r--. 1 root root 0 8月 13 22:54 centos
[root@localhost mytest]# ll | tee suse | head -2 \\將ll命令輸出的內(nèi)容豌研,復(fù)制一份到suse妹田,然后繼續(xù)交由下一個(gè)命令處理
總用量 12
-rw-r--r--. 1 root root 0 8月 13 22:54 centos
[root@localhost mytest]# cat suse
總用量 12
-rw-r--r--. 1 root root 0 8月 13 22:54 centos
-rw-r--r--. 1 root root 56 8月 13 23:33 fedora
-rw-r--r--. 1 root root 51 8月 13 23:26 suse
-rw-r--r--. 1 root root 106 8月 13 22:57 ubuntu
[root@localhost mytest]# ls | tee suse | grep centos
centos
[root@localhost mytest]# cat suse
centos
fedora
suse
ubuntu
[root@localhost mytest]# ls fedora | tee -a suse | tr [a-z] [A-Z] \\此處tr處理的字符是前一個(gè)命令的輸出通過(guò)管道符做為輸入交給其處理,tee -a追加到文件鹃共。
FEDORA
[root@localhost mytest]# cat suse
centos
fedora
suse
ubuntu
fedora
[root@localhost mytest]#