linux資料流重新導向

來源:互聯網
上載者:User

linux資料流重新導向
1. 標準輸入和輸出執行一個命令,有輸入,有輸出。 標準輸入: standard input ,檢查 stdin , 代碼為0, 使用< 或 << 標準輸出: standard output, 簡稱:stdout . 命令執行返回的正確結果, 代碼為1, 使用 > 或 >> 標準錯誤輸出; standard error output. 簡稱:stderr, 命令執行返回的錯誤資訊。 代碼為2, 使用 > 或 >> 預設都輸出到螢幕上。
2. 資料流輸出重新導向輸出預設為螢幕, 可以將輸出重新導向到檔案或其他裝置。 使用 > 或 >> > : 表示覆蓋寫入 >> : 表示追加到原檔案末尾,原檔案為空白則建立檔案。

$ find /home -name .bashrc                 find: /home/lost+found: Permission denied     //stderr/home/user/.bashrc     //stdout
使用 > 將內容重新導向到檔案
$ find /home -name .bashrc > std_out 2> std_err
將查詢結果寫入到std_out檔案 將標準錯誤寫入到std_err檔案 注意 :2> 或 2>> 中間不能有空格。
/dev/null 目錄表示空裝置, 寫入這兒的內容會被丟棄。 如果不想儲存錯誤資訊可以寫入/dev/null
$ find /home -name .bashrc > std 2> /dev/null
想把正確和錯誤的寫入一個檔案,使用 2>&1 或 &>
$ find /home -name .bashrc > std_out 2>&1$ find /home -name .bashrc  &> std_out 
一般建議使用第一種寫法。
3. 輸入重新導向stdin 為鍵盤輸入,使用cat
[work@www sh]$ cat > catfilehello this is from cat .#按Ctrl+d[work@www sh]$ 
<為把鍵盤輸入改為檔案內容輸入
[work@www sh]$ cat > catfile < pass
<< 表示結束輸入。
[work@www sh]$ cat > catfile << "eof"> hello,> this is from cat > eof[work@www sh]$ cat catfile hello,this is from cat [work@www sh]$
當鍵盤輸入eof結束輸入,而不必輸入Ctrl+d.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.