第十六章 輸出輸入重定向,管道技術
阿新 • • 發佈:2020-08-01
輸出輸入
重定向
什麼是重定向
將一些資訊重定向到一個指定的地方,或者從一個重定向的位置讀取內容。
為什麼使用重定向?
1.當螢幕輸出的資訊很重要,而且希望儲存重要的資訊時;
2.後臺執行中的程式,不希望他干擾螢幕正常的輸出結果時;
3.系統的例行命令, 例如定時任務的執行結果,希望可以存下來時;
4.一些執行命令,我們已經知道他可能出現錯誤資訊, 想將他直接丟棄時;
5.錯誤日誌與正確日誌需要分別輸出至不同的檔案儲存時;
[root@lxy ~]# ll /dev/st*
lrwxrwxrwx. 1 root root 15 Nov 28 18:49 /dev/stderr -> /proc/self/fd/2 #錯誤輸出 2> 2>> 不能省略
lrwxrwxrwx. 1 root root 15 Nov 28 18:49 /dev/stdin -> /proc/self/fd/0 #標準輸入 < << 0 可以省略
lrwxrwxrwx. 1 root root 15 Nov 28 18:49 /dev/stdout -> /proc/self/fd/1 #標準輸出 > >> 1 可以省略
#檢視程式的進行過程中產生的一些檔案
[root@lxy ~]# tail -f /var/log/messages
Dec 19 15:01:01 lxy systemd: Created slice User Slice of root.
Dec 19 15:01:01 lxy systemd: Started Session 538 of user root.
Dec 19 15:01:01 lxy systemd: Removed slice User Slice of root.
Dec 19 16:01:01 lxy systemd: Created slice User Slice of root.
Dec 19 16:01:01 lxy systemd: Started Session 539 of user root.
^Z
[1]+ Stopped tail -f /var/log/messages #將程序轉至後臺
[root@lxy ~]# ps
PID TTY TIME CMD
97761 pts/0 00:00:00 su
97767 pts/0 00:00:00 bash
97968 pts/0 00:00:00 tail
97974 pts/0 00:00:00 ps
[root@lxy ~]# ll /proc/97968/fd/
total 0
lrwx------. 1 root root 64 Dec 19 16:25 0 -> /dev/pts/0
lrwx------. 1 root root 64 Dec 19 16:25 1 -> /dev/pts/0
lrwx------. 1 root root 64 Dec 19 16:24 2 -> /dev/pts/0
lr-x------. 1 root root 64 Dec 19 16:25 3 -> /var/log/messages
lr-x------. 1 root root 64 Dec 19 16:25 4 -> anon_inode:inotify
#Linux檢視標準輸入輸出裝置
[root@lxy ~]# ls -l /dev/std*
lrwxrwxrwx 1 root root 15 Dec 2 22:30 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 Dec 2 22:30 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 Dec 2 22:30 /dev/stdout -> /proc/self/fd/1
stderr #標準錯誤輸出 2 預設輸出到螢幕上面
stdin #標準輸入 0 預設是由鍵盤或者從命令或者檔案中讀取
stdout #標準輸出 1 預設輸出到螢幕上面
[root@lxy ~]# cat
oldboy #標準輸入,沒有指定檔案時,預設從鍵盤中讀取資料
oldboy #標準輸出,將你標準輸入的結果進行標準輸出到螢幕上面
輸出重定向
作用:將輸出的結果重定向到一個指定的位置中,(不管正確的資訊或者錯誤的資訊)
> 標準輸出重定向,作用:覆蓋原始檔的內容,將新的正確的內容寫入到檔案中,如果檔案不存在,會自動建立。
>> 標準輸出追加重定向,作用:不會覆蓋原始檔的內容,會將新正確的的內容追加到檔案的底部,如果檔案不存在,會自動建立。
2> 標準錯誤輸出重定向,作用:覆蓋原始檔的內容,將新的錯誤的資訊重定向到檔案中,如果檔案不存在,會自動建立。
2>> 標準錯誤輸出追加重定向,作用:不會覆蓋原始檔的內容,將新的錯誤的資訊追加到檔案的底部,如果檔案不存在,會自動建立。
案例1: 標準輸出重定向
[root@lxy ~]# echo hello
hello
[root@lxy ~]# echo hello >file1.txt #標準輸出重定向, 先清空,後寫入, 如果檔案不存在則建立
[root@lxy ~]# cat file1.txt
hello
[root@lxy ~]# cat file1.txt /etc/passwd >file2.txt
[root@lxy ~]# cat file2.txt
hello
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@lxy ~]# cat file1.txt >file3.txt #合併兩個檔案,將兩個檔案的輸出重新定向到一個新的檔案中
[root@lxy ~]# cat file3.txt
hello
[root@lxy ~]# cat >file4.txt
oldboy
oldgirl
^C
[root@lxy ~]# cat file4.txt
oldboy
oldgirl
[root@lxy ~]# cat >>file4.txt
linux
^C
[root@lxy ~]# cat file4.txt
oldboy
oldgirl
linux
案例2:標準輸出追加重定向
[root@lxy ~]# echo 'test' >>file1.txt #向配置檔案末尾追加內容
[root@lxy ~]# cat file1.txt
hello
test
案例3:標準錯誤輸出重定向
[root@lxy ~]# ls /rttt >file5.txt
ls: cannot access /rttt: No such file or directory
[root@lxy ~]# cat file5.txt
[root@lxy ~]# ls /rttt 2>file5.txt #錯誤的輸出每次會覆蓋檔案中的內容,正確的內容不會輸出
[root@lxy ~]# cat file5.txt
ls: cannot access /rttt: No such file or directory
[root@lxy ~]# ls /root 2>file5.txt
file1.txt file2.txt file3.txt file4.txt file5.txt
案例4:標準錯誤輸出追加重定向
[root@lxy ~]# ls /rttt 2>>file5.txt #錯誤的輸出會往檔案的尾部在新增內容
[root@lxy ~]# cat file5.txt
ls: cannot access /rttt: No such file or directory
ls: cannot access /rttt: No such file or directory
[root@lxy ~]# ls /rttt 2>>file5.txt
[root@lxy ~]# cat file5.txt
ls: cannot access /rttt: No such file or directory
ls: cannot access /rttt: No such file or directory
ls: cannot access /rttt: No such file or directory
[root@lxy ~]# ls /root 2>>file5.txt
file1.txt file2.txt file3.txt file4.txt file5.txt
[root@lxy ~]# cat file5.txt
ls: cannot access /rttt: No such file or directory
ls: cannot access /rttt: No such file or directory
ls: cannot access /rttt: No such file or directory
案例5:將錯誤和正確的都重定向到一個檔案中
[root@lxy ~]# ls /rttt >file6.txt 2>&1 #不推薦
[root@lxy ~]# ls /root >file6.txt 2>&1
[root@lxy ~]# cat file6.txt
file1.txt
file2.txt
file3.txt
[root@lxy ~]# ls /rttt &>file6.txt #推薦寫法
[root@lxy ~]# cat file6.txt
ls: cannot access /rttt: No such file or directory
[root@lxy ~]# ls /root &>file6.txt
[root@lxy ~]# cat file6.txt
file1.txt
file2.txt
file3.txt
[root@lxy ~]# cat file6.txt
ls: cannot access /rot: No such file or directory
[root@lxy ~]# ls /rot &>>file6.txt
[root@lxy ~]# cat file6.txt
ls: cannot access /rot: No such file or directory
ls: cannot access /rot: No such file or directory
[root@lxy ~]# ls /root &>>file6.txt
[root@lxy ~]# cat file6.txt
ls: cannot access /rot: No such file or directory
ls: cannot access /rot: No such file or directory
file1.txt
file2.txt
file3.txt
案例6: 將輸出的資訊定向到空
[root@lxy ~]# ls /root
file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt
[root@lxy ~]# echo $?
0
[root@lxy ~]# ls /root >/dev/null
[root@lxy ~]# cat /dev/null
[root@lxy ~]# ls /rtt 2>/dev/null
[root@lxy ~]# ls /rtt &>/dev/null
[root@lxy ~]# ls /root &>/dev/null
案例7: 將正確的輸出資訊和錯誤的輸出資訊重定向不同的檔案中
[root@lxy ~]# ls /opt/ 2>file7.txt
services
[root@lxy ~]# ls /opt/ >file7.txt 2>file8.txt
[root@lxy ~]# rm -rf /opt/
[root@lxy ~]# ls /opt/ >file7.txt 2>file8.txt
[root@lxy ~]# cat file7.txt
[root@lxy ~]# cat file8.txt
ls: cannot access /opt/: No such file or directory
[root@lxy ~]# ls /opt/ >>file7.txt 2>>file8.txt
[root@lxy ~]# rm -rf /opt/
[root@lxy ~]# ls /opt/ >>file7.txt 2>>file8.txt
[root@lxy ~]# cat file7.txt
services
[root@lxy ~]# cat file8.txt