linux各種資料流重新導向

來源:互聯網
上載者:User
  • > :以覆蓋的方法將『正確的資料』輸出到指定的檔案或裝置上
  • 1> :以覆蓋的方法將『正確的資料』輸出到指定的檔案或裝置上
  • 1>>:以累加的方法將『正確的資料』輸出到指定的檔案或裝置上
  • 2> :以覆蓋的方法將『錯誤的資料』輸出到指定的檔案或裝置上
  • 2>>:以累加的方法將『錯誤的資料』輸出到指定的檔案或裝置上
  • <:將原本需要由鍵盤輸入的資料,改由檔案內容來取代
  • <<:代表的是『結束的輸入字元』

例如:『我要用 cat 直接將輸入的資訊輸出到 catfile 中, 且當由鍵盤輸入 eof 時,該次輸入就結束』,那我可以這樣做:

[root@www ~]# cat > catfile << "eof"> This is a test.> OK now stop> eof  <==輸入這關鍵詞,立刻就結束而不需要輸入 [ctrl]+d[root@www ~]# cat catfileThis is a test.OK now stop

其中eof可以使用其他的任一字元串代替。還有一個“<<-”,主要是為了在輸出是忽略掉語句前面的一個或者多個tab符,看下面的例子:

#!/bin/bashcat << EOF123456EOF

輸出為:

123456如果改成:
#!/bin/bashcat <<- EOF123456EOF

輸出為:

123456

  • 2>& 1:將正確和錯誤的資料全部寫入到指定檔案或裝置上
例如:
yan@yan-vm:~$ ll /root/ /home/ > result 2>& 1yan@yan-vm:~$ cat result/home/:total 16drwxr-xr-x  4 root       root        4096 Jun  4 13:03 ./drwxr-xr-x 23 root       root        4096 Apr 12 20:43 ../drwx------  2 normaluser normalgroup 4096 Jun  4 13:14 normaluser/drwxr-xr-x 24 yan        yan         4096 Jun  5 06:36 yan/ls: cannot open directory /root/: Permission denied

使用者yan無法訪問/root,所以Permission denied是錯誤資料,其他的都是正確的資料。

下面我們來衍生幾種用法,大家猜猜看運行結果是什麼:
ll /root/ /home/ > result 1>&2
ll /root/ /home/ 2> result 1>&2
ll /root/ /home/ 2>&1 > result 

結果:

yan@yan-vm:~$ ll /root/ /home/ > result 1>&2/home/:total 16drwxr-xr-x  4 root       root        4096 Jun  4 13:03 ./drwxr-xr-x 23 root       root        4096 Apr 12 20:43 ../drwx------  2 normaluser normalgroup 4096 Jun  4 13:14 normaluser/drwxr-xr-x 24 yan        yan         4096 Jun  5 06:36 yan/ls: cannot open directory /root/: Permission deniedyan@yan-vm:~$ cat resultyan@yan-vm:~$

分析:正確的資料合併到錯誤的資料,將正確的資料寫到result中,但是此時正確的資料為空白,所以寫入到result檔案中為空白,

將錯誤的資料(包含正確的資料)顯示到螢幕上。
yan@yan-vm:~$ ll /root/ /home/ 2> result 1>&2yan@yan-vm:~$ cat result/home/:total 16drwxr-xr-x  4 root       root        4096 Jun  4 13:03 ./drwxr-xr-x 23 root       root        4096 Apr 12 20:43 ../drwx------  2 normaluser normalgroup 4096 Jun  4 13:14 normaluser/drwxr-xr-x 24 yan        yan         4096 Jun  5 06:36 yan/ls: cannot open directory /root/: Permission denied

分析:正確的資料合併到錯誤的資料,將錯誤的資料寫到result中,此時錯誤的資料中包含正確的資料,所以result中包含正確和錯誤的資料。

將正確的資料寫到螢幕上,但是此時正確的資料為空白,所以沒有任何資訊顯示在螢幕上。
yan@yan-vm:~$ ll /root/ /home/ 2>&1 > resultls: cannot open directory /root/: Permission deniedyan@yan-vm:~$ cat result/home/:total 16drwxr-xr-x  4 root       root        4096 Jun  4 13:03 ./drwxr-xr-x 23 root       root        4096 Apr 12 20:43 ../drwx------  2 normaluser normalgroup 4096 Jun  4 13:14 normaluser/drwxr-xr-x 24 yan        yan         4096 Jun  5 06:36 yan/yan@yan-vm:~$

分析:沒有這種用法,所以2>&1被忽略。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.