標籤:shell linux stdin stdout stderr
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
[[email protected] sh]$ cat > catfilehello this is from cat .#按Ctrl+d[[email protected] sh]$
<為把鍵盤輸入改為檔案內容輸入
[[email protected] sh]$ cat > catfile < pass
<< 表示結束輸入。
[[email protected] sh]$ cat > catfile << "eof"> hello,> this is from cat > eof[[email protected] sh]$ cat catfile hello,this is from cat [[email protected] sh]$
當鍵盤輸入eof結束輸入,而不必輸入Ctrl+d.
地址:http://blog.csdn.net/yonggang7/article/details/40956095
linux資料流重新導向