linux中的重定向符和管道(<,><<,>>,|)
阿新 • • 發佈:2022-05-18
管道與重定向的使用
標準輸入的檔案描述符為0.
標準輸出的檔案描述符為1
錯誤輸出的檔案描述符為2
管道
管道:可以讓我們將多條命令連線在一起。
作用:將一個命令的標準輸出重定向給下一個命令,並作為該命令的標準輸入。
(base) [root@localhost ~]# ifconfig ens33|grep 'inet' 過濾包含ip地址的行 inet 192.168.72.10 netmask 255.255.255.0 broadcast 192.168.72.255 (base) [root@localhost ~]#
重定向
輸出重定向可以使用>或>>符號
> :可以將輸出匯入到檔案,如果檔案不存在,則建立該檔案,如果檔案已經存在,則會覆蓋該檔案的內容;
>>:可以將輸出追加到檔案
對於錯誤資訊對的重定向
>2 >>2
程式碼示例:
(base) [root@localhost ~]# rpm -qa|grep gcc #查詢計算機中是否安裝了gcc (base) [root@localhost ~]# echo "pass" |passwd --stdin yan #設定yan 的密碼為pass (base) [root@localhost ~]# ls #檢視當前檔案列表 anaconda2 dr-elephant-feature_spark2.3-12.1 node_modules (base) [root@localhost ~]# ls > list.txt #將輸出儲存至list.txt,螢幕無輸出 (base) [root@localhost ~]# hostname >> list.txt #將主機名追加到list.txt檔案尾部 (base) [root@localhost ~]# mail -s test [email protected] <list.txt #發郵件,郵件內容來自list.txt (base) [root@localhost~]# ls -l abc anaconda-ks.cfg #檢視檔案詳細資訊,abc不存在 ls: cannot access abc: No such file or directory -rw-------. 1 root root 1234 Nov 16 2020 anaconda-ks.cfg (base) [root@localhost ~]# ls -l abc anaconda-ks.cfg 2>error.txt #僅將錯誤從定向,不影響輸出。 -rw-------. 1 root root 1234 Nov 16 2020 anaconda-ks.cfg (base) [root@localhost ~]# ls -l abc anaconda-ks.cfg > all 2>&1 #標準輸出與錯誤輸出都匯入到all (base) [root@localhost ~]# ls -l abc anaconda-ks.cfg >> all 2>&1 #標準輸出與錯誤輸出追加到all (base) [root@localhost ~]# ls -l abc anaconda-ks.cfg &> all #標準輸出與錯誤輸出都匯入到all