管道與重定向的使用
標準輸入的檔案描述符為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
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/477333.html
標籤:Linux
上一篇:Centos 安裝 docker
